I made a system, where user can create table for the bank, and tbm can create columns, so I do not know which columns were created, but with the bank script I can list the columns of this table
I'm trying to mount my view table with datatables server side
declaration of datatable js:
$(document).ready(function(){
$('table').DataTable({
"order": [[ 1, "asc" ]],
"scrollCollapse": true,
responsive: true,
dom: '<"html5buttons"B>lTfgitp',
buttons: [
{extend: 'excel', title: 'ExampleFile'},
{extend: 'pdf', title: 'ExampleFile'}
],
"processing": true,
"serverSide": true,
"ajax":{
url :"ajax.php", // json datasource
type: "post", // method , by default get
success: function(res){
return res;
},
error: function(err){ // error handling
console.log("error", err);
}
},
<?php
echo "columns: [";
$query2 = "select COLUMN_NAME from information_schema.columns
where table_schema = '$table_schema' AND table_name = '$nList'
order by table_name,ordinal_position";
$query2 = mysql_query($query2);
$i=0;
$n = mysql_num_rows($query2);
while ($res2 = mysql_fetch_array($query2)) {
$value = $res2["COLUMN_NAME"];
$i++;
if ($i == $n) {
echo '{ "title": "'.$value.'" }';
}else{
echo '{ "title": "'.$value.'" },';
}
}
echo "]";
?>
});
});
where $ nList is the table name and $ my table_schema
Now it's in my ajax.php
:
$start = $_REQUEST["start"];
$length = $_REQUEST["length"];
$query = "SELECT * FROM patient_records ORDER BY id DESC LIMIT $start, $length";
$query = mysql_query($query);
$rows = array();
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
}
$totaldata = mysql_num_rows($query);
$totalfiltered = mysql_num_rows($query);
$json_data = array(
"draw" => intval( $_REQUEST['draw'] ),
"recordsTotal" => intval( $totaldata ),
"recordsFiltered" => intval( $totalfiltered ),
"data" => $rows
);
echo json_encode($json_data);
In html, the datatable shows the columns, but the data does not:
Onmyconsole,soitreturnsfromtheajax.phpdata
Can you help me?