请问PHP生成JSON分页问题
最近在搞生成PHP 生成JSON,想实现分页,下边代码已能把数组生成了,分页要怎实现了?想实现每10条分一次页
<?php
//connection information
$host = "XXX";
$user = "XXX";
$password = "XX";
$database = "XX";
//make connection
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);
//query the database
mysql_query('set character set "utf8"');
$query = mysql_query("SELECT * FROM akb order by infoid desc");
//start json object
$json = '{"tags":[';
//loop through and return results
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$row = mysql_fetch_assoc($query);
$infoid=$row["InfoId"];
$Title=$row["Title"];
$pic=$row["LogoImg"];
$bpic=$row["LogoImg2"];
$time=$row["AccTime"];
$info=$row["Burden"];
$url=$row["ExtUrl"];
$json .= '{"id":"'. $infoid .'","title":"'.$Title.'","pic":"'.$pic.'","bpic":"'.$bpic.'","time":"'.$time.'","info":"'. $info .'","url":"'. $url.'"}';
if ($x < mysql_num_rows($query) -1)
$json .= ",";
else
$json .= "]}";
}
$response = $_GET["callback"] . $json;
echo $response;
//close connection
mysql_close($server);
?>
------解决方案--------------------你可先分页,再生成json.
------解决方案--------------------先生成数组,再用json_encode()生成json。
------解决方案--------------------分页不是在客户端用JS分的,分页通常都在服务器端用PHP计算出需要获取的数据库条数,然后直接从数据库中获取选择的记录。
比如你通过GET或POST方法指导了现在是第几页,
$page = 1;//第几页
$pagenum = 10;//每页几条
$start = ($page - 1) * $pagenum;//这个就是计算出开始的记录数
//你的SQL语句就可以这样写
$sql = "SELECT * FROM akb order by infoid desc LIMIT $start,$pagenum";
------解决方案--------------------前端部分,比如用jquery库