문제

Im trying to parse pager data and grid data through a json response into a jqgrid.

Here is the php file that creates a json file wich contains all these data (pager+grid data)

            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);

With this php code i retrieve a json file like this :

[{"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"}}]

But this is wrong structure. In a post here in stackoverflow user Musa said that the structure must be like this :

{
    "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"
            ]
        }
    ]
}

Can someone help me fix my code so the structure that json response be right ?

Thanks for any help!

도움이 되었습니까?

해결책

So finally i solve this problrm using php's stdClass to load the data and here is the code:

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top