Sorting Issue

Sorting Issue

RookieProgrammerRookieProgrammer Posts: 4Questions: 3Answers: 0

hi Everyone,

I Am trying to sort the datatable. i am taking dynamic Variable in "Order by Field" however it's not sorting out so kindly guide if i am missing something...

<?php include_once 'db_config.php'; ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ERROR); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src=""></script> <script src=""></script> <link rel="stylesheet" type="text/css" href="//"/> <link rel="stylesheet" href="" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title>Pagination-2</title> </head> <body> <div class="container"> <table id="example" class="table table-hover table-striped"> <thead> <tr> <th>User Id</th> <th>User name</th> <th>First Name</th> <th>Last Name</th> <th>Email </th> <th>Gender</th> </tr> </thead> </table> </div> <script> $(document).ready(function() { $('#example').DataTable( { //"processing": true, "serverSide": true, "searching":true, "order":[], "ajax": { 'url':"pagi.php", 'method':"POST", }, // "columns": [ // {"name": "user_id"}, // {"name": "fname"}, // {"name": "lname"}, // {"name": "eml"}, // {"name": "gnd"} // ] }); }); </script> </body> </html>

include_once 'db_config.php';

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

$start = $_POST['start'];
$end = $_POST['length'];
//echo $columnIndex;
//echo $columnSortOrder;

ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

//echo "SELECT * FROM user_list WHERE (user_name LIKE '%$strInput%' OR fname LIKE '%$strInput%' OR lname LIKE '%$strInput%') ORDER BY '$columnIndex' '$columnSortOrder' LIMIT $start,$end";

$sql = $conn->query("SELECT * FROM user_list WHERE (user_name LIKE '%$strInput%' OR fname LIKE '%$strInput%' OR lname LIKE '%$strInput%') ORDER BY '$columnIndex' '$columnSortOrder' LIMIT $start,$end");  
//print_r ($sql);


$sql1 = $conn->query("SELECT * FROM user_list");  
//$page_result = $sql;  
$total_records = $sql1->rowCount(); 
$data = array();
while($row = $sql->fetch()){ 
    $data[] = array(
$output  = array(
    'draw' => $_POST['draw'],
    'recordsTotal' => $total_records,
    'recordsFiltered' => $total_records,
    'data' => $data,
echo json_encode($output);

Best Regards,
Jay Patel

This question has an accepted answers - jump to answer


  • allanallan Posts: 54,880Questions: 1Answers: 8,601 Site admin
    Answer ✓

    You have serverSide enabled, so all sorting, fitlering and paging need to be done by the server.

    If you have less than 10k rows, then just remove the serverSide option. If you have more than that, then have a look at this blog post which shows how you can use our Editor libraries to do server-side processing. Or your can implement it yourself using the manual page I linked to above.


Sign In or Register to comment.