jQgrid错误的寻呼机结构

时间:2013-06-15 20:09:06

标签: php json data-structures pager jqgrid-php

我试图通过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响应的结构正确吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所以最后我用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);