How to pass value for where-clause filtering in server-side processing?
How to pass value for where-clause filtering in server-side processing?
Hello. We are trying to use PHP and DataTables' server-side processing to display only a single ballot's entries from a MySQL table that contains many votes from many ballots -- and then order them with the most recent votes at the top.
We have successfully extracted all data from the table and get it to show within DataTables' resulting web page. But we're stumped on how to pass the value for the where clause and sort order data to the ssp.class.php file referenced in the DataTables Examples for server-side processing.
Here is the javascipt in the main HTML page:
$(document).ready(function() {
$('#entries').DataTable( {
dom: "frtip",
paging: true,
responsive: true,
processing: true,
serverSide: true,
ajax: {
"url": "./controller_entries_data_ssp_dfoaijerioqjdoifaodfijq.php",
"data": {
"Ballot_ID": <?php echo($Ballot_ID); ?>
}
},
search: {
return: true,
},
pagingType: "numbers",
select: true
} );
} );
Is this the correct way to pass the Ballot ID to the server-side processing script?
We are using the server-side processing script in the DataTables Examples section for server-side processing, only modifying the columns section as follows:
$columns = array(
array( 'db' => 'Entry_ID', 'dt' => 0 ),
array( 'db' => 'Ballot_ID', 'dt' => 1, 'search' => 1419 ),
array(
'db' => 'Ballot_DateTimeCast',
'dt' => 2,
'formatter' => function( $d, $row ) {
return date( 'M j, Y - h:i:s a', strtotime($d));
}
),
array( 'db' => 'Voter_Name', 'dt' => 3 ),
array( 'db' => 'Voter_EMail', 'dt' => 4 ),
array( 'db' => 'Ballot_Disqualified', 'dt' => 5 ),
array( 'db' => 'Verification_Status', 'dt' => 6 ),
array( 'db' => 'Comments', 'dt' => 7 ),
array( 'db' => 'Entry_Category_01', 'dt' => 8 ),
array( 'db' => 'Entry_Category_02', 'dt' => 9 ),
array( 'db' => 'Entry_Category_03', 'dt' => 10 ),
array( 'db' => 'Entry_Category_04', 'dt' => 11 ),
array( 'db' => 'Entry_Category_05', 'dt' => 12 ),
Our experiments with adding a search value to the Ballot_ID column are not working (see the third line of the code above). 1419 is the Ballot_ID value we're testing (we're not sure how to access the value passed in the additional ajax data in the javascript). What is the proper way of doing this?
The code in ssp.class.php file references building both a WHERE and an ORDER BY phrase in the server-side processing, so I assume this is possible. Any guidance would be appreciated.
Thank you!
);
This question has an accepted answers - jump to answer
Answers
To follow up on this question, it is referring to the first example code posted on the Datatables.net site for server-side processing:
https://datatables.net/examples/server_side/simple.html
The ssp.class.php file is what Allen posted in the comment on that example.
Thanks again.
Looks fine to me. It isn't secure, so if you are worried about people accessing data for other Ballot_IDs, then you wouldn't use that method (it would need to be fully server-side), but that's fine for a basic use case.
If you want to apply a WHERE condition, then use the
SSP::complex
method - see here.Allan
Thank you, Allan. We successfully used the SSP::complex method you suggested, and the DataTables portion of this project is working perfectly. (Very slick!)
However, we don't see how to apply Editor in this case. We have a separate test page that uses Editor (client-side) that works beautifully. But the data changes are being ignored when they are submitted in the server-side processing project. No errors are apparent.
The successful client-side server Editor uses this code to apply changes to the test database:
This is completely different from the server-side processing script of :
The Editor server-side processing example at https://editor.datatables.net/examples/simple/server-side-processing.html shows code similar to the first code above. How would we incorporate that functionality into the server-side code that uses your ssp.class.php library?
Thank you again!
If you are using Editor throw away the SSP class - you don't need it. The Editor libraries have server-side processing support built in.
Allan
That did the trick. Thank you!