Rename values using if else

2

I am using DataTable to generate a table where it has a Status field that receives values 1 or 2 and Status with value 1 is Active and 2 is Inactive. how do I show Active / Inactive instead of #

Try to do if else here to print the result but I could not.

This is where I make array of Status: array ('db' = > 'status', 'dt' = > 6),

obs. db is the result that comes from the database and dt is the column in the datatable.

    $columns = array(
    array( 'db' => 'id', 'dt' => 0),
    array( 'db' => 'empresa', 'dt' => 1),
    array( 'db' => 'ar',  'dt' => 2),
    array( 'db' => 'nf',   'dt' => 3),
    array( 'db' => 'tp', 'dt' => 4),
    array( 'db' => 'descricao', 'dt' => 5,
        'formatter' => function( $d,$row ){
            return substr (($d), 0, 46);
        }
        ),
    array( 'db' => 'status', 'dt' => 6),


    array( 'db' => 'created','dt' => 7,
        'formatter' => function( $d, $row ) {
            return date( 'd-m-Y - H:i:s', strtotime($d));
        }
    ),
    array( 'db' => 'modified','dt' => 8,
        'formatter' => function( $d, $row ) {
            return date( 'd-m-Y - H:i:s', strtotime($d));
        }
    )       

);

Thank you very much!

    
asked by anonymous 21.07.2017 / 16:11

1 answer

4

Create a function as you did for the other fields if you want you can use a ternary to do the comparison or if.

The difference is that it checks if the value is 1 is active any other is inactive. Now with if you can make a comparison.

Another alternative is to control this with array. If new statuses are added they will see elements of the array and not a new if.

Ternary:

'formatter' => function($d, $row ){
            return $d == 1 ? 'Ativo' : 'Inativo';
        }

if:

'formatter' => function($d, $row ){
            if($d == 1) return 'ativo';
            else if($d == 2) return 'inativo';
            else return 'desconhecido';
        }

Array:

'formatter' => function($d, $row){
            $status = array(1 => 'Ativo', 2 => 'Inativo');
            return isset($status[$d]) ? $status[$d] : 'desconhecido';
        }
    
21.07.2017 / 16:20