phpmysqlssp

Change empty strings to Null or "None" ssp class with PHP and Mysql


How to convert empty string to Null or "Nothing" when the column appears in the datatable. For example, in the brand column, there are many empty strings. So, i wanted to change it to null or "None".

$columns = array(
    array( 'db' => 'id', 'dt' => 0 ),
    array( 'db' => 'name',      'dt' => 1 ),
    array( 'db' => 'brand',     'dt' => 2 ),
    array(
        'db'        => 'end-date',
        'dt'        => 3,
        'formatter' => function( $d, $row ) {
            return date( 'jS M Y', strtotime($d));
        }
    ),
    array(
        'db'        => 'id',
        'dt'        => 4,
        'formatter' => function( $d, $row ) {
            return '<a class="btn btn-danger" href="proDelete.php?id=' . $d . '" onclick="return confirm(\'Are you sure you want to delete this item?\')">Delete</a>';
        }
    ),
);

$where = "end-date< CURDATE()";
// Include SQL query processing class
require( 'ssp.class.php' );

// Output data as json format
echo json_encode(
    SSP::complex( $_GET, $dbDetails, $table, $primaryKey, $columns, $where)
);

Solution

  • Just use a function like you do it in other columns:

    array(
        'db' => 'brand',
        'dt' => 2,
        'formatter' => function($d, $row) {
            return $d !== '' ? $d : 'Nothing';
        }
    ),