php分页功能
1<?php
2//header("Content-Type: text/html; charset=utf-8");
3$link=mysql_connect("localhost","root","");
4mysql_select_db("db");
5mysql_query("set names utf8");
6$sql="select * from useinfo";
7$result=mysql_query($sql);
8$num=mysql_num_rows($result);
9$page=$_GET['page'];//当前页
10if(empty($page)||$page<=0)
11 $page=1;
12$page_len=9;//显示的页数
13$page_total=ceil($num/10);//总页数
14$begin=1;//第一页
15$end=$page_total;
16if($page!=1)
17{
18 $pagenums="$page/$page_total <a href="/" mce_href="/""".$_SERVER['PHP_SELF'].'?page=1">首页 </a>';
19 $pagenums.="<a href="/" mce_href="/""".$_SERVER['PHP_SELF']."?page=".($page-1).'">上一页 </a>';
20}
21else
22 $pagenums="$page/$page_total 首页 上一页 ";
23$temp=($page-1)*10;
24$sql="select * from useinfo limit $temp,10";
25$result=mysql_query($sql);
26while($row=mysql_fetch_array($result))
27 echo $row['title']."<br>";
28if($page_total>$page_len)
29{
30 if($page+4>$page_total)
31 {
32 $end=$page_total;
33 $begin=$page_total-8;
34 }
35 else if($page<=5)
36 {
37 $begin=1;
38 $end=9;
39 }
40 else
41 {
42 $begin=$page-4;
43 $end=$page+4;
44 }
45}
46for($i=$begin;$i<=$end;$i++)
47{
48 if($page!=$i)
49 $pagenums.='<a href="'.$_SERVER['PHP_SELF']." mce_href="'.$_SERVER['PHP_SELF']."?page=$i/" >$i </a>";
50 else
51 $pagenums.="$i ";
52}
53if($page==$page_total)
54$pagenums.="下一页 末页";
55else
56$pagenums.="<a href="/" mce_href="/""".$_SERVER['PHP_SELF']."?page=".($page+1)."/">下一页 </a>"."<a href="/" mce_href="/""".$_SERVER['PHP_SELF']."?page=$page_total/" >末页</a>";
57echo $pagenums;
58?>
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/php-paging/4175.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.