1. 分页的前提是记录按id排序, 且不连续, 比如有些记录被删除,
或者要分页显示查找结果, 这样就有了除分页外的条件$q
2. 确定分页的方式:
(1): 用简单的"页首, 上一页, 下一页".
(2): 用"1,2,3,4,5,6,..........末尾"来指定跳到某页.
3. 实现分析:
(1) 如果先查询全部结果, 只显示其中的部分. 这种方式显然不好,
会累坏server.
(2) 对于用limit m,n实现分页, 有些不负责, 服务器在实际操作时还是
按$q条件找出所有结果, 然后只返回m后的n条. server工作仍然很多.
(3) 优化的办法是知道要显示页的起始$id, 查询
"where $q and id>=$id order by id desc limit 0,$page_length"
这样mysql 会先按id的索引找到符合条件的id, 然后再评估$q.
(4) 那$id怎么来呢?
(5) 对于显示方式1, 每页多查询一条,最后一个记录的$id就是啦
"where $q and id>=$id order by id limit 0,$page_length+1"
if (mysql_num_rows($result) > $page_length) echo "下一页"
//(记住最后一条记录不要显示!)
//如果不使用第二种分页方式, 到此结束.
(5) 对于显示方式2, 后面$page_offset=6页的每页起始id要一次知道.
"select id from xxxx where $q order by id desc limit 0,$page_length*$page_offset"
for($i=0;$i*$page_length<$mysql_num_rows($result);$i++){
$start=mysql_result($result,$i*$page_length,0);
echo '<a href=../../"xxxxx?pageno='.($i+1)."&id=$start\">";
if ($id==$start) echo "<b>$i</b>"; //加重显示当前页号
&
| 对此文章发表了评论 |
