How to edit ssp.class.php for custom HTTP variables (date range filter)
How to edit ssp.class.php for custom HTTP variables (date range filter)
I am trying to use the date range filter example with server-side. I am adding a filter for date on "reg_dato" field. I understand I need to change the code in server-processing.php according to this post but do I also need to change some code in the ssp.class.php?
I also read in the linked post that It may be better to use the editor libraries? I am using editor on other pages. I could not find any examples of edited ssp.class.php? I want to display dates in the MIN and MAX field as DD-MM-YY but need to send this to the server script as YYYY-MM-DD for mySQL.
- I had to write this to send the value in correct format for mySQL. Do you know why?
my client code:
// Create date inputs
minDate = new DateTime($('#min'), {
format: 'DD-MM-YYYY'
});
maxDate = new DateTime($('#max'), {
format: 'DD-MM-YYYY'
});
// Refilter the table
$('#min, #max').on('change', function () {
table.draw(); //alert('draw: ' + $('#minDate.val()'));
});
table = $('#example').DataTable( {
processing: true,
serverSide: true,
ajax: {
url: '/admin/ajax/server_processing.php',
data: function (d) {
d.min = moment($('#min').val()).format('YYYY-DD-MM'); // this format sends to server script as YYYY-MM-DD
d.max = moment($('#max').val()).format('YYYY-DD-MM');
},
},
In chrome - network tab I can see the value sent:
search[regex]: false
min: 2022-08-02
max: Invalid date
_: 1661174931785
In server_processing.php I try to capture the variable for min and max:
$extraWhere = '';
if(isset($_GET['min'])){
$extraWhere .= " AND reg_dato >= " $_GET['min'];
};
if(isset($_GET['min'])){
$extraWhere .= " AND reg_dato <= " $_GET['min'];
};
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $extraWhere )
This fails with the last line and I am not surprised since this is probably not the way to enter extra sql clauses.
- How to I write the last line to capture the extra sql? Where do I put $extraWhere in ssp.class.php? Or should I use the EDITOR libraries for this?
Answers
Hi,
Personally I'd use the Editor libraries over the SSP class every time as I like its fluent interface, it is documented and fully supported.
However, SSP class has the benefit of being fairly simple to read and understand.
In this case, the
SSP::simple
method does not have a parameter for theextraWhere
. There is aSSP::complex
method for that.One small complexity there - you don't want the
AND
at the start of the string you are passing in. Consider the case when there is no other filter - it would end up asWHERE AND reg_dato ...
which would throw an SQL error.I'd probably use an array for the conditions and then
join
them withAND
as the joining expression to solve that.Allan