分页原理
/**
* @Author: anchen
* 分页原理 列表页
*/
require("include/conn.php");
$page = isset($_GET['page']) ? intval($_GET['page']):1;//当前页码
$pagesize = 5;//每页显示记录条数
$offset = ($page-1)*$pagesize;//偏移量
$query = "select * from 007_news";//取出记录总数和总页数
$result = mysql_query($query);
$totalNum = mysql_num_rows($result);
$totalPageCount = ceil($totalNum/$pagesize);
//构建分页的SQL语句:每次显示的是10条记录
$sql = "select * from 007_news order by id desc limit $offset,$pagesize";
$result = mysql_query($sql);
//上一页
$prev = ($page -1 <=0)?1:$page-1;
//下一页
$next = ($page+1 >=$totalPageCount) ? $totalPageCount:$page+1;
while($arr=mysql_fetch_assoc($result)) {
$rows[]=$arr;
}
//var_dump($rows);exit;
$list = "<table >";
foreach($rows as $key =>$val) {
$list .= "<tr>";
$list .= "<td>".$val['id']."</td>";
$list .= "<td>".$val['title']."</td>";
$list .= "<td>".$val['author']."</td>";
$list .= "<td>".$val['source']."</td>";
$list .="</tr>";
}
$list .="</table>";
echo $list;
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1\">首页</a>";
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$prev."\">上一页</a>";
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$next."\">下一页</a>";
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$totalPageCount."\">尾页</a>";