0

在我的 javascript 中,我的这个 json 工作得很好。

{
    "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"]
    ] 
}

但这当然是硬编码的,我想让 aaData 动态。我打算做类似 $.ajax 我的 php 有这个代码

$result = mysql_query("SELECT * FROM Persons");

$newArray = array();
while($row =mysql_fetch_array($result) ){
    $newArray[] = $row;
}

echo json_encode($newArray);

来自 json_endcode 的数据是

[
{"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"}
]

请注意它有 [{ …}] 而不是 [[…]] 所以如果我替换我的 aaData (硬编码的,上面有这个),我会出错。如何使我的 php 代码返回 aaData 中的内容。谢谢

4

2 回答 2

1

因为您提供关联数组来列出。

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}

尝试替换为

$newArray[] = array_values($row);
于 2012-07-30T20:42:52.433 回答
0

如我所见,您正在使用datatables. 您必须
返回sEchoiTotalRecords和。iTotalDisplayRecordsaaData

参考

于 2012-07-30T20:40:14.637 回答