0

jpgridを使い始めたばかりです。私はphpjson_encodeでこの問題を抱えています。

jqgrid Webサイトの例によると、json文字列は次のようなphpコードで生成できます。

    $responce->page = "1";
    $responce->total = 1;
    $responce->records = "1";

    $i=1; $amttot=0; $taxtot=0; $total=0;
    while($i<=10) {
        $amttot += $row[amount];
        $taxtot += $row[tax];
        $total += $row[total];
        $responce->rows[$i]['id']=strval($i);
        $responce->rows[$i]['cell']=array("123","2012-10-15","name","10000","tax","9999","note");
        $i++;
    }
    $responce->rows = (array)$responce->rows;
    $responce->userdata['amount'] = $amttot;
    $responce->userdata['tax'] = $taxtot;
    $responce->userdata['total'] = $total;
    $responce->userdata['name'] = 'Totals:';

私は問題を抱えています

    $responce->rows[$i]['id']=strval($i);
    $responce->rows[$i]['cell']=array("123","2012-10-15","name","10000","tax","9999","note");

php json_encodeがjson文字列を生成する場合、rows配列は連想配列として扱われます。したがって、結果のjson文字列は次のようになります。

{"page":"1","total":1,"records":"1",
 "rows":{"1":{"id":"1","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "2":{"id":"2","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "3":{"id":"3","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "4":{"id":"4","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "5":{"id":"5","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "6":{"id":"6","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "7":{"id":"7","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "8":{"id":"8","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "9":{"id":"9","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
         "10":{"id":"10","cell":["123","2012-10-15","name","10000","tax","9999","note"]}},
 "userdata":{"amount":0,"tax":0,"total":0,"name":"Totals:"}}

このjson文字列はjqgridでは処理できません。実際、jqgridはjavascriptエラーを発生させません。テーブルの内容をレンダリングしないだけです。jqgridサンプルWebサイトからfirebugsを介してjson文字列を確認しました。次のようになります。

{"page":"1","total":2,"records":"13",
 "rows":[{"id":"13","cell":["13","2007-10-06","Client 3","1000.00","0.00","1000.00",null]},
         {"id":"12","cell":["12","2007-10-06","Client 2","700.00","140.00","840.00",null]},
         {"id":"11","cell":["11","2007-10-06","Client 1","600.00","120.00","720.00",null]},
         {"id":"10","cell":["10","2007-10-06","Client 2","100.00","20.00","120.00",null]},          
         {"id":"9","cell":["9","2007-10-06","Client 1","200.00","40.00","240.00",null]},
         {"id":"8","cell":["8","2007-10-06","Client 3","200.00","0.00","200.00",null]},
         {"id":"7","cell":["7","2007-10-05","Client 2","120.00","12.00","134.00",null]}, 
         {"id":"6","cell":["6","2007-10-05","Client 1","50.00","10.00","60.00",""]},
         {"id":"5","cell":["5","2007-10-05","Client 3","100.00","0.00","100.00","no tax at all"]},
         {"id":"4","cell":["4","2007-10-04","Client 3","150.00","0.00","150.00","no tax"]}],
        "userdata":{"amount":3220,"tax":342,"total":3564,"name":"Totals:"}}

連想配列を使用してjson文字列内にそのような配列objを生成するphpサンプルコードをどのように作成できるかわかりません。私はphp5.4を使用しています

これを機能させるために、jqgridまたはphpで何を変更できますか?私は他の誰もこの問題を抱えていなかったことに驚いています。私はかなり明白な何かを見逃しているかもしれません。

ありがとう

キャスビー

4

1 に答える 1

0

この問題は、PHP での応答配列オブジェクトの生成方法が原因で発生します。最初にループで配列を作成したとき、インデックスを 0 ではなく 1 から開始しました。 Response->rows[i] これにより、php は応答配列をシーケンシャルではなく連想配列として扱いました。したがって、phpは名前値オブジェクトを応答オブジェクトに入れます(名前のないオブジェクトの配列を作成しようとしている間)

解決策は、配列インデックスが連続的かつ連続的であることを確認することです

于 2012-10-17T20:39:09.863 に答える