Reputation: 121
I tried to parse database table data to jquery data table which are dynamically change. I got following output after php's json_encode
function
$sql = "SELECT * FROM login";
$result = mysql_query($sql);
$a= array();
while ($row = mysql_fetch_assoc($result)) {
$a[][] = $row;
}
echo (json_encode($a));
Json output
[[{"id":"1","username":"test11","password":"$2y$10$NiKnEN\/ww8yGVhv3JNjSuO5FfOFSthadS2B3GcbA3KGBktAOSu6lK","role":"Administrator "}],[{"id":"2","username":"test","password":"test","role":"test"}]]
Then I called jquery data table ajax function as they said. here my coding for that
$('#example').dataTable( {
"ajax": 'ab.php'
} );
But ultimately display only "Loading..." text in the jquery data table tbody
section. Why is that?
Upvotes: 2
Views: 21160
Reputation: 58900
Apparently you're using DataTables version 1.10. By default, this version expects data to be in certain format, see DataTables documentation for more information.
{
"data": [
// row 1 data source,
// row 2 data source,
// etc
]
}
Change $a[][] = $row;
to $a['data'][] = $row
in your PHP to produce data in the correct format.
Upvotes: 4
Reputation: 591
If you using datatable 1.10 api, you need to create a json response like :
$json_data = array(
"draw" => intval( $_REQUEST['draw'] ),
"recordsTotal" => intval( $totaldata ),
"recordsFiltered" => intval( $totalfiltered ),
"data" => $data
);
echo json_encode($json_data);
draw: we send same number which has been send by datatable at time of draw/request.
recordsTotal: Total numbers of records in your table.
recordsFiltered: Total numbers of filtered records after searching in your table. If you do not perform any search then recordsFiltered will be same as recordsTotal.
data: Your fetched records data. You have to fetched the data as per start, length, search value, colomn name and sorting orders parameters. you can download dummy database table from here
Please refer this link to know the details
Upvotes: 4