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 (ハードコードされたもの、上記のもの) を置き換えるとエラーになります。aaDataにあるものを返すようにphpコードを作成するにはどうすればよいですか。ありがとう

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。、、および
を返す必要があります。sEchoiTotalRecordsiTotalDisplayRecordsaaData

参照

于 2012-07-30T20:40:14.637 に答える