Datatable server-side: Soting column A-Z/Z-A using php query

Datatable server-side: Soting column A-Z/Z-A using php query

michelmirmichelmir Posts: 16Questions: 7Answers: 0

Hello. I would like to know how can i improve the php code below to sorting column correctly on server-side method? My Datatable is not sorting column correctly: Below is the peace of my php query code:

$query = '';
$output = array();
$query .= "SELECT * FROM users ";
if(isset($_POST["search"]["value"]))
{
    $query .= 'WHERE first_name LIKE "%'.$_POST["search"]["value"].'%" ';
    $query .= 'OR last_name LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["order"]))
{
    $query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
    $query .= 'ORDER BY id DESC ';
}

The code line ($query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';) above, should sort column correctly but when i try to click on sort icon in column header, the order gets confused not sort correctly like A-Z or Z-A. What could it be?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,686Questions: 1Answers: 10,100 Site admin

    Can you echo out the created SQL (or otherwise log it) and show it to me please? I don't immediately see anything obviously wrong with the above.

    That said, if you are using server-side processing, you might be as well using the demo SSP class which will deal with search and sort for you.

    Allan

  • allanallan Posts: 61,686Questions: 1Answers: 10,100 Site admin
    Answer ✓

    Perhaps you could echo out the SQL that is being generated when the use clicked on one of the name columns. That is where I would start (as suggested before :)).

    Allan

This discussion has been closed.