码迷,mamicode.com
首页 > 其他好文 > 详细

分页原理

时间:2016-07-12 18:54:41      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * @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>";

分页原理

标签:

原文地址:http://www.cnblogs.com/lilyhomexl/p/5664275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!