0
i made a system, where the user can create table for the database, and tbm can create columns, so I don’t know which columns were created, but with the database script I can list the columns of this table
I am trying to assemble my view table with datatables server side
datatable js statement:
$(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 $table_schema my DB
now I was 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, datatable shows columns, but data does not:
in my console, the return ta of ajax.php data
can help me?
thank you worked
– Furlan