0

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

mario
  • 144,265
  • 20
  • 237
  • 291
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
  • The first block of code that you show is not a JSON string, nor a representation of Javascript Object Notation. It is an array that holds a collection of arrays. echoing json_encode on the array that you're creating out of your MySQL result will output a valid JSON string. You need to deal with that string accordingly. using `$.getJSON()` will get you that object (JSON parsed and all), then you can work on using `$.each` to iterate over the collections of that JSON string. – Ohgodwhy Jul 30 '12 at 20:38
  • possible duplicate of [Getting correct JSON format](http://stackoverflow.com/questions/11447582/getting-correct-json-format) – mario Jul 30 '12 at 20:39

2 Answers2

1

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);
message
  • 4,513
  • 2
  • 28
  • 39
0

As I can see you're using datatables.
You have to return sEcho, iTotalRecords, iTotalDisplayRecords and aaData.

Reference

Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82