jQgrid错误的寻呼机结构
Posted
技术标签:
【中文标题】jQgrid错误的寻呼机结构【英文标题】:jQgrid wrong pager structure 【发布时间】:2013-06-15 20:09:06 【问题描述】:我试图通过 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"]
但这是错误的结构。在 *** 的一篇文章中,用户 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 响应的结构正确吗?
感谢您的帮助!
【问题讨论】:
【参考方案1】:所以最后我解决了这个问题,使用 php 的 stdClass 加载数据,这里是代码:
init_mysql();
$response = new stdClass();
// Getting pages number (for jqGrid pager)
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;
$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
// Getting data for jqGrid table
$data = mysql_query("SELECT * FROM logs ORDER BY log_id DESC");
$i = 0;
if(mysql_num_rows($data))
while($row = mysql_fetch_array($data))
$response->rows[$i]['id']=$i+1;
$response->rows[$i]['cell']=array('log_id'=>$row['log_id'],'ip'=>$row['ip'],'hostname'=>$row['host'],'log'=>$row['input'],'date'=>$row['date']);
$i++;
echo json_encode($response);
【讨论】:
以上是关于jQgrid错误的寻呼机结构的主要内容,如果未能解决你的问题,请参考以下文章