In my javascript I have this json working just fine.
{
"sEcho": 1,
"iTotalRecords": 58,
"iTotalDisplayRecords": 58,
"aaData": [
["Gecko","Firefox 1.0","Win 98+ / OSX.2+","1.7","A"],
["Gecko","Firefox 1.5","Win 98+ / OSX.2+","1.8","A"],
["Gecko","Firefox 2.0","Win 98+ / OSX.2+","1.8","A"],
["Gecko","Firefox 3.0","Win 2k+ / OSX.3+","1.9","A"],
["Gecko","Camino 1.0","OSX.2+","1.8","A"],
["Gecko","Camino 1.5","OSX.3+","1.8","A"],
["Gecko","Netscape 7.2","Win 95+ / Mac OS 8.6-9.2","1.7","A"],
["Gecko","Netscape Browser 8","Win 98SE+","1.7","A"],
["Gecko","Netscape Navigator 9","Win 98+ / OSX.2+","1.8","A"],
["Gecko","Mozilla 1.0","Win 95+ / OSX.1+","1","A"]
]
}
But this is of course hard coded and I want to make aaData dynamic . I am planning on doing something like $.ajax my php has this code
$result = mysql_query("SELECT * FROM Persons");
$newArray = array();
while($row =mysql_fetch_array($result) ){
$newArray[] = $row;
}
echo json_encode($newArray);
and the data from json_endcode is
[
{"0":"1","P_Id":"1","1":"zamor","LastName":"zamor","2":"credit","FirstName":"credit","3":"Giftcard","Address":"Giftcard","4":"Finance","City":"Finance"},{"0":"3","P_Id":"3","1":"zamor3","LastName":"zamor3","2":"credit","FirstName":"credit","3":"Giftcard","Address":"Giftcard","4":"Finance","City":"Finance"},{"0":"4","P_Id":"4","1":"zamor4","LastName":"zamor4","2":"credit","FirstName":"credit","3":"Giftcard","Address":"Giftcard","4":"Finance","City":"Finance"},{"0":"5","P_Id":"5","1":"zamor5","LastName":"zamor5","2":"credit","FirstName":"credit","3":"Giftcard","Address":"Giftcard","4":"Finance","City":"Finance"},{"0":"2","P_Id":"2","1":"zamor2","LastName":"zamor2","2":"credit","FirstName":"credit","3":"Giftcard","Address":"Giftcard","4":"Finance","City":"Finance"}
]
notice that it has [{ …}] instead of [[…]] so if I replace my aaData (hardcoded one, with this above) it goes me error. How can I make my php code to return something that is in aaData. thanks
Because you providing associative array to list.
php > $simple = array(1,2,3,4,5);
php > $assoc = array('a'=> 1, 'b' => 2);
php > echo json_encode($simple);
[1,2,3,4,5]
php > echo json_encode($assoc);
{"a":1,"b":2}
try to replace with
$newArray[] = array_values($row);
As I can see you're using datatables
.
You have to return sEcho
, iTotalRecords
, iTotalDisplayRecords
and aaData
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.