Compare with if between two columns

Compare with if between two columns

alansilvaalansilva Posts: 1Questions: 1Answers: 0

Hello, how could I compare between two columns?

        $columns = array(
            array( 'db' => 'id', 'dt' => 0 ),
                            array( 'db' => 'price', 'dt' => 1 ),
            array( 'db' => 'qtd_min',  'dt' => 2 ),
            array(
                'db'        => 'qtd',
                'dt'        => 3,
                'formatter' => function( $d, $row ) {
                    if($d < 'qtd_min') {   // I don't know how to do it
                                            return ' - '.$d;
                                        }
                }
            )
        );

Thanks in advance

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited December 2017

    I have absolutely no idea what you are donig there @alansilva but this code echos my two silly messages :smile:

    $columns = array(
        array( 'db' => 'id',        'dt' => 0 ),
        array( 'db' => 'price',     'dt' => 1 ),
        array( 'db' => 'qtd_min',   'dt' => 2 ),
        array( 'db' => 'qtd',       'dt' => 3 )
    );
    
    if ( $columns[0]["db"] < $columns[1]["db"] ) {
        echo ('id is smaller than price!' . '<br>'); 
    }
    if ( $columns[0]["dt"] < $columns[1]["dt"] ) {
        echo ('0 is smaller than 1!'); 
    }
    
  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    The $row parameter has the rest of the row in it, so you would use:

    'formatter' => function ( $d, $row ) {
      return $d < $row['qtd_min'] ? $d*-1 : $d;
    }
    

    Allan

This discussion has been closed.