How To Add A Where Condition To Server Side Processing PHP
How To Add A Where Condition To Server Side Processing PHP
I need to ad a "WHERE" condition to my results. But am to sure where to add it exactly.
If I were writing straight PHP I would add this: to my query: WHERE user_id = '2'
But with using DataTables, I am to sure where to add that condition. I did read through the information o this page: https://editor.datatables.net/manual/php/conditions#Simple-usage
But that does not tell you where you should add that ... it just gives an example like $editor->where( 'user_id', 12 );
and doesn't give any examples of where you would use that ... I presume in the server_processing.php file.
I am using server side processing and my script looks like this:
<?php
/*
* DataTables example server-side processing script.
* @license MIT - http://datatables.net/license_mit
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
// DB table to use
$table = 'links';
// Table's primary key
$primaryKey = 'id';
// 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' => 'id', 'dt' => 0 ),
array( 'db' => 'title', 'dt' => 1 ),
array( 'db' => 'url', 'dt' => 2 ),
array( 'db' => 'created', 'dt' => 3 )
);
// SQL server connection information
$sql_details = array(
'user' => 'xxx',
'pass' => '*xxx',
'db' => 'xxx',
'host' => '108.xxx.xxx.xxx'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
This question has an accepted answers - jump to answer
Answers
This thread should help, it's asking the same thing.
Cheers,
Colin
@colin
That was perfect! It worked and only took about 3 seconds to implement.
Thank you!
For others who may be reading this and are looking for an answer to the same question ...
If you take a look at line 50 in the above code snippet ... it reads:
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
I changed that to read
SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns, null, "user_id = '2'" )
"user_id" is the name of the column WHERE I want to look for all the entries by "2" ...