1

I am working with DataTables and it requires a specific format for the JSON object in order to populate the tables.

It should look like this:

{"aaData": [
        [ "Trident", "Internet Explorer 5.1", "Win 95+", 4, "X" ],
        [ "Trident", "Internet Explorer 5.0", "Win 95+", 5, "C" ],
        [ "Trident", "Internet Explorer 5.5", "Win 95+", 5.5, "A" ],
        [ "Trident", "Internet Explorer 6.0", "Win 98+", 6, "A" ],
        [ "Trident", "Internet Explorer 7.0", "Win XP SP2+", 7, "A" ]
}

I have tried hundred's of different ways, but cannot get to something that looks like the above and so DataTables does not accept it.

This is one of my latest attempts:

$aaData["aaData"] = array(array());
$i=0;
while($r= mysql_fetch_assoc($sql)){
    $aaData[$i][] = $r["data1"];
            $aaData[$i][] = $r["data2"] ;
            $aaData[$i][] = $r["data3"] ;
    $aaData[$i][] = $r["data4"] ;
            $aaData[$i][] = $r["data5"] ;

            $i++;
}
$aaData=json_encode($aaData);
echo $aaData;

This give me an JSON response from the server ( as verified by Firebug) that looks like this:

{"aaData":[
[]],
"0":["data1","data2","data3","data4","data5"],
"1":["data11","data21","data31","data41","data51"],
etc.....]
}

So, I do not need the keys ( 0,1...) nor do I need the "[]]" stuff. I did look at similar post here and everywhere, but did not find anything that helps me with this. How can I get rid of them? Thanks for your assistance.

1 Answer 1

2

use this

$aaData["aaData"] = array();
//$i=0;
while($r= mysql_fetch_assoc($sql)){
    $arr= array();
    $arr[] = $r["data1"];
    $arr[] = $r["data2"] ;
    $arr[] = $r["data3"] ;
    $arr[] = $r["data4"] ;
    $arr[] = $r["data5"] ;
    array_push($aaData["aaData"],$arr);
        //$i++;
}
$aaData=json_encode($aaData);
echo $aaData;

working example http://codepad.viper-7.com/gk2DO3

Sign up to request clarification or add additional context in comments.

7 Comments

just a typing accident. I am still verifying your code.Sorry for that. Thanks.
Yogesh. Thanks but that does not work it either. Here what it gives me. {"aaData":[],"0":["data1"],"1":["data2"],"2":["data3"],"3":["data4"],"4":["data5"],"5":["data11"],"6":["data21"],"7":["data31"],"8":["data41"],"12":["data51"] etc....
Thanks, Yogesh. Almost there. There's one "[" too many enveloping that whole thing. If you look the result at codepad.viper {"aaData":[[0,0,0,0,0],[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4]]}, the first "[" and the last "]" should not be there, so it will be the same as required by DataTable.
@BernardA Its JSON my friend, not a string. It has limitation. We can't remove the [].
Hi Yogesh. I see what you are saying, but still those guys at DataTable demand it and it seems that their users manage somehow to get it done as required. Thank your for your help so far. I will keep on trying.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.