PHP分页显示的方法分析【附PHP通用分页类】

本文实例讲述了PHP分页显示的方法。分享给大家供大家参考,具体如下:

  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. $currentpage = 1;
  4. if(isset($_GET['page']))
  5. $currentpage = $_GET['page'];
  6. //连接数据库
  7. $link = mysql_connect("localhost","root","") or die('连接失败');
  8. mysql_select_db('myschool');
  9. mysql_query('set names utf8');
  10. $sql ="SELECT count(*) as 'count' from student";//查询记录的sql语句
  11. $result = mysql_query($sql);
  12. $arr = mysql_fetch_array($result);
  13. $count = $arr['count'];
  14. $pagesize = 3;
  15. $pages = ceil($count/$pagesize);//共多少页
  16. $prepage = $currentpage -1;
  17. if($prepage<=0)
  18. $prepage=1;
  19. $nextpage = $currentpage+1;
  20. if($nextpage >= $pages){
  21. $nextpage = $pages;
  22. }
  23. $start =($currentpage-1) * $pagesize;//起始位置
  24. $sql = "SELECT * from student limit $start,$pagesize";
  25. echo $sql;
  26. // $sql = "select * from student";
  27. $result = mysql_query($sql);
  28. ?>
  29. <!-- html部分 -->
  30. <!DOCTYPE html>
  31. <html >
  32. <head>
  33. <meta charset="UTF-8">
  34. <title>Document</title>
  35. </head>
  36. <body>
  37. <table >
  38. <tr>
  39. <td>学号</td>
  40. <td>姓名</td>
  41. <td>性别</td>
  42. <td>年龄</td>
  43. </tr>
  44. <?php while($arr=mysql_fetch_array($result)){ ?>
  45. <td><?php echo $arr['number']; ?></td>
  46. <td><?php echo $arr['name']; ?></td>
  47. <td><?php echo $arr['sex']; ?></td>
  48. <td><?php echo $arr['age']; ?></td>
  49. </tr> //phpfensi.com
  50. <?php } ?>
  51. </table>
  52. <a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$prepage; ?>" rel="external nofollow" >上一页</a> <a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$nextpage; ?>" rel="external nofollow" >下一页</a>
  53. </body>
  54. </html>

注:当一个文件中有php和html两种时,php文件必须有结束标记

附:php通用分页类与用法:

Page.class.php文件:

  1. <?php
  2. /**
  3. * 分页类
  4. *
  5. * 调用方式:
  6. * $p=new Page(总条数,显示页数,当前页码,每页显示条数,[链接]);
  7. * print_r($p->getPages()); //生成一个页码数组(键为页码,值为链接)
  8. * echo $p->showPages(1); //生成一个页码样式(可添加自定义样式)
  9. *
  10. */
  11. /*
  12. 总条数,需要显示的页数,当前页,每页显示的条数,连接
  13. 生成一个一维数组,键为页码 值为连接
  14. 返回一个生成好样式的页码(并且可以根据自己需要添加样式)
  15. 默认样式 共45条记录,每页显示10条,当前第1/4页 [首页] [上页] [1] [2] [3] .. [下页] [尾页]
  16. */
  17. class Page{
  18. protected $count; //总条数
  19. protected $showPages; //需要显示的页数
  20. protected $countPages; //总页数
  21. protected $currPage; //当前页
  22. protected $subPages; //每页显示条数
  23. protected $href; //连接
  24. protected $page_arr=array(); //保存生成的页码 键页码 值为连接
  25. /**
  26. * __construct 构造函数(获取分页所需参数)
  27. * @param int $count 总条数
  28. * @param int $showPages 显示页数
  29. * @param int $currPage 当前页数
  30. * @param int $subPages 每页显示数量
  31. * @param string $href 连接(不设置则获取当前URL)
  32. */
  33. public function __construct($count,$showPages,$currPage,$subPages,$href=''){
  34. $this->count=$count;
  35. $this->showPages=$showPages;
  36. $this->currPage=$currPage;
  37. $this->subPages=$subPages;
  38. //如果链接没有设置则获取当前连接
  39. if(emptyempty($href)){
  40. $this->href=htmlentities($_SERVER['PHP_SELF']);
  41. }else{
  42. $this->href=$href;
  43. }
  44. $this->construct_Pages();
  45. }
  46. /**
  47. * getPages 返回页码数组
  48. * @return array 一维数组 键为页码 值为链接
  49. */
  50. public function getPages(){
  51. return $this->page_arr;
  52. }
  53. /**
  54. * showPages 返回生成好的页码
  55. * @param int $style 样式
  56. * @return string 生成好的页码
  57. */
  58. public function showPages($style=1){
  59. $func='pageStyle'.$style;
  60. return $this->$func();
  61. }
  62. /**
  63. * pageStyle1 分页样式(可参照这个添加自定义样式 例如pageStyle2())
  64. * 样式 共45条记录,每页显示10条,当前第1/4页 [首页] [上页] [1] [2] [3] .. [下页] [尾页]
  65. * @return string
  66. */
  67. protected function pageStyle1(){
  68. /* 构造普通模式的分页
  69. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [1] [2] [3] .. [下页] [尾页]
  70. */
  71. $pageStr='共'.$this->count.'条记录,每页显示'.$this->subPages.'条';
  72. $pageStr.='当前第'.$this->currPage.'/'.$this->countPages.'页 ';
  73. $_GET['page'] = 1;
  74. $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>] </span>';
  75. //如果当前页不是第一页就显示上页
  76. if($this->currPage>1){
  77. $_GET['page'] = $this->currPage-1;
  78. $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上页</a>] </span>';
  79. }
  80. foreach ($this->page_arr as $k => $v) {
  81. $_GET['page'] = $k;
  82. $pageStr.='<span>[<a href="'.$v.'" rel="external nofollow" >'.$k.'</a>] </span>';
  83. }
  84. //如果当前页小于总页数就显示下一页
  85. if($this->currPage<$this->countPages){
  86. $_GET['page'] = $this->currPage+1;
  87. $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下页</a>] </span>';
  88. }
  89. $_GET['page'] = $this->countPages;
  90. $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾页</a>] </span>';
  91. return $pageStr;
  92. }
  93. /**
  94. * construct_Pages 生成页码数组
  95. * 键为页码,值为链接
  96. * $this->page_arr=Array(
  97. * [1] => index.php?page=1
  98. * [2] => index.php?page=2
  99. * [3] => index.php?page=3
  100. * ......)
  101. */
  102. protected function construct_Pages(){
  103. //计算总页数
  104. $this->countPages=ceil($this->count/$this->subPages);
  105. //根据当前页计算前后页数
  106. $leftPage_num=floor($this->showPages/2);
  107. $rightPage_num=$this->showPages-$leftPage_num;
  108. //左边显示数为当前页减左边该显示的数 例如总显示7页 当前页是5 左边最小为5-3 右边为5+3
  109. $left=$this->currPage-$leftPage_num;
  110. $left=max($left,1); //左边最小不能小于1
  111. $right=$left+$this->showPages-1; //左边加显示页数减1就是右边显示数
  112. $right=min($right,$this->countPages); //右边最大不能大于总页数
  113. $left=max($right-$this->showPages+1,1); //确定右边再计算左边,必须二次计算
  114. for ($i=$left; $i <= $right; $i++) {
  115. $_GET['page'] = $i;
  116. $this->page_arr[$i]=$this->href.'?'.http_build_query($_GET);
  117. }
  118. }
  119. }
  120. ?>

用法示例demo.php:

  1. /**
  2. * demo
  3. */
  4. header("content-type:text/html;charset=utf8");
  5. include('Page.class.php'); //引入类
  6. //$p=new Page(总条数,显示页数,当前页码,每页显示条数,[链接]);
  7. //连接不设置则为当前链接
  8. $page=isset($_GET['page']) ? $_GET['page'] : 1;
  9. $p=new Page(100,4,$page,8);
  10. //生成一个页码数组(键为页码,值为链接)
  11. echo "<pre>";
  12. print_r($p->getPages());
  13. //样式 共45条记录,每页显示10条,当前第1/4页 [首页] [上页] [1] [2] [3] .. [下页] [尾页]
  14. echo $p->showPages(1);