Rename values using if Else

Asked

Viewed 84 times

2

I am using Datatable to generate a table where there is 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 the number.

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

This snippet is where I array the 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!

  • I couldn’t locate the Status column you commented on.

1 answer

4


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

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

Another alternative is to control this with array. If new status is added they see array elements 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';
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.