Skip to content
Advertisement

How to use ‘WHERE’ clause using ssp.class.php DataTables

Okay so i’m trying to display information from my database using jQuery DataTable (DataTables.net). I can get it to work fine displaying the entire table ‘notes’ but I would like to just display notes that have not been read. So I need to include a WHERE clause somehow but i’m unclear on the best way to go about this.

Here is how I currently display the whole table:

// DB table to use
$table = 'Notes';

// Table's primary key
$primaryKey = 'CID';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'CID', 'dt' => 0 ),

array(
    'db'        => 'CID',
    'dt'        => 0,
    'formatter' => function( $d, $row ) {
        return '<a href="profile.php?search='.$d.'" target="_Blank">'.$d."</a>";
    }
),

array( 'db' => 'Title', 'dt' => 1 ),
array( 'db' => 'Name',  'dt' => 2 ),
array(
    'db'        => 'Date',
    'dt'        => 3,
    'formatter' => function( $d, $row ) {
        return date( 'jS M y', strtotime($d));
        }
    )
);

// SQL server connection information
$sql_details = array(
'user' => '*DB_USER*',
'pass' => '*Password*',
'db'   => '*DatabaseName*',
'host' => 'localhost'
);
require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

I would need the equivalent of SELECT * FROM Notes WHERE Status ='Unread'

Advertisement

Answer

You should change DataTables default functions to do this!

use this ssp.class.php customized class

Link

Use it like following example:

require( 'ssp.class.php' );
$where = "Status ='Unread'";
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns,$where )
);

If you set $where parameter, Custom class will add where clause to select statement!

Update :

DataTables in 2015 add complex method

The new built-in method can set where clause in query!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement