我试图通过 json 响应将寻呼机数据和网格数据解析到 jqgrid 中。
这是创建包含所有这些数据(寻呼机+网格数据)的 json 文件的 php 文件
if(!$sidx) $sidx =1;
$result = mysql_query("SELECT COUNT(*) AS count FROM logs");
$row = mysql_fetch_array($result);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit;
$pages_array = array("page"=> $page , "total" => $total_pages , "records" => $count);
array_push($return_array,$pages_array);
// Getting data for jqGrid table
$data = mysql_query("SELECT * FROM logs ORDER BY log_id DESC");
if(mysql_num_rows($data))
{
while($row = mysql_fetch_array($data))
{
$row_array['rows']['log_id'] = $row['log_id'];
$row_array['rows']['ip'] = $row['ip'];
$row_array['rows']['hostname'] = $row['host'];
$row_array['rows']['log'] = $row['input'];
$row_array['rows']['date'] = $row['date'];
array_push($return_array,$row_array);
}
}
echo json_encode($return_array);
使用这个 php 代码,我检索一个像这样的 json 文件:
[{"page":"1","total":2,"records":"34"},
{"rows":{"log_id":"108","ip":"127.0.0.1","hostname":"","log":"having 1=1--","date":"09-06-2013 22:05:57"}},
{"rows":{"log_id":"107","ip":"127.0.0.1","hostname":"","log":"\/\/","date":"09-06-2013 22:05:57"}},
{"rows":{"log_id":"106","ip":"127.0.0.1","hostname":"","log":"**/","date":"09-06-2013 22:05:55"}},
{"rows":{"log_id":"105","ip":"127.0.0.1","hostname":"","log":"+and+","date":"09-06-2013 22:05:55"}}]
但这是错误的结构。在 stackoverflow 的一篇文章中,用户 Musa 说结构必须是这样的:
{
"page": 2,//current page
"total": 2,//number of pages
"records": 11,//# of records in total
"rows": [//array of data
{
"id": "101",//id for this row of data
"cell": [
"101",
"Sundale",
"OTTP",
"652",
"6",
"65",
"656665",
"986346654",
"823343454",
"554332"
]
}
]
}
有人可以帮我修复我的代码,以便 json 响应的结构正确吗?
谢谢你的帮助!