How to solve DATE_FORMAT sorting issue in server_processing.php

How to solve DATE_FORMAT sorting issue in server_processing.php

MicLueMicLue Posts: 1Questions: 0Answers: 0
edited November 2011 in DataTables 1.8
After a couple of hours spending my time to try out DataTables, i've found an issue, that was also discussed in this forum.
The problem was, that if you use DATE_FORMAT in your query for a special format, MySQL delivers only the formatted string and DataTables will sort this string by numbers, not by date.

So we have to make sure that our original column names are known by DataTables.

My solution to this is:

[code]
// Example with 7 columns. 1st column results in a concatination fo 4 fields from the database, 2nd and 4th are formattet, all others are normal

$aColumns = array( 'CONCAT(some,columns,to,concat)', "DATE_FORMAT(someDateField,'%d.%m.%Y')", 'time1', "DATE_FORMAT(otherDateField,'%d.%m.%Y')", 'time2', 'target1', 'target2' );

// set column-names seperately, if you use MySQL formatting like DATE_FORMAT, count exactly (= 7)
// 1st could be sorted by the resulting string, 2nd and 4th must be sorted as an date, so ordering must know the real fieldname for MySQL, all other are handled normal

$aOrders = array(NULL,'someDateField',NULL,'otherDateField',NULL,NULL,NULL);

[/code]

Looking further down you will see my new declaration for ordering. Watch for $aColumns and $aOrders...

[code]
/*
* Ordering
*/
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i
This discussion has been closed.