PHP实现的简单分页类及用法示例

本文实例讲述了PHP实现的简单分页类及用法。分享给大家供大家参考,具体如下:

  1. <?php
  2. /*
  3. * 使用:
  4. * $page = new Page(连接符,查询语句,当前页码,每页大小,页码符)
  5. * 连接符:一个MYSQL连接标识符,如果该参数留空,则使用最近一个连接
  6. * 查询语句:SQL语句
  7. * 当前页码:指定当前是第几页
  8. * 每页大小:每页显示的记录数
  9. * 页码符:指定当前页面URL格式
  10. *
  11. * 使用例子:
  12. * $sql = "select * from aa";
  13. * $page = new Page($conn,$sql,$_GET['page'],4,"?page=");
  14. *
  15. * 获得当前页码
  16. * $page->page;
  17. *
  18. * 获得总页数
  19. * $page->pageCount;
  20. *
  21. * 获得总记录数
  22. * $page->rowCount;
  23. *
  24. * 获得本页记录数
  25. * $page->listSize;
  26. *
  27. * 获得记录集
  28. * $page->list;
  29. * 记录集是一个2维数组,例:list[0]['id']访问第一条记录的id字段值.
  30. *
  31. * 获得页码列表
  32. * $page->getPageList();
  33. */
  34. class Page
  35. {
  36. //基础数据
  37. var $sql;
  38. var $page;
  39. var $pageSize;
  40. var $pageStr;
  41. //统计数据
  42. var $pageCount; //页数
  43. var $rowCount; //记录数
  44. //结果数据
  45. var $list = array(); //结果行数组
  46. var $listSize ;
  47. //构造函数
  48. function Page($conn,$sql_in,$page_in,$pageSize_in,$pageStr_in)
  49. {
  50. $this->sql = $sql_in;
  51. $this->page = intval($page_in);
  52. $this->pageSize = $pageSize_in;
  53. $this->pageStr = $pageStr_in;
  54. //页码为空或小于1的处理
  55. if(!$this->page||$this->page<1)
  56. {
  57. $this->page = 1;
  58. }
  59. //查询总记录数
  60. $rowCountSql = preg_replace("/([\w\W]*?select)([\w\W]*?)(from[\w\W]*?)/i","$1 count(0) $3",$this->sql);
  61. if(!$conn)
  62. $rs = mysql_query($rowCountSql) or die("bnc.page: error on getting rowCount.");
  63. else
  64. $rs = mysql_query($rowCountSql,$conn) or die("bnc.page: error on getting rowCount.");
  65. $rowCountRow = mysql_fetch_row($rs);
  66. $this->rowCount=$rowCountRow[0];
  67. //计算总页数
  68. if($this->rowCount%$this->pageSize==0)
  69. $this->pageCount = intval($this->rowCount/$this->pageSize);
  70. else
  71. $this->pageCount = intval($this->rowCount/$this->pageSize)+1;
  72. //SQL偏移量
  73. $offset = ($this->page-1)*$this->pageSize;
  74. if(!$conn)
  75. $rs = mysql_query($this->sql." limit $offset,".$this->pageSize) or die("bnc.page: error on listing.");
  76. else
  77. $rs = mysql_query($this->sql." limit $offset,".$this->pageSize,$conn) or die("bnc.page: error on listing.");
  78. while($row=mysql_fetch_array($rs))
  79. {
  80. $this->list[]=$row;
  81. }
  82. $this->listSize = count($this->list);
  83. }
  84. /*
  85. * getPageList方法生成一个较简单的页码列表
  86. * 如果需要定制页码列表,可以修改这里的代码,或者使用总页数/总记录数等信息进行计算生成.
  87. */
  88. function getPageList()
  89. {
  90. $firstPage;
  91. $previousPage;
  92. $pageList;
  93. $nextPage;
  94. $lastPage;
  95. $currentPage;
  96. //如果页码>1则显示首页连接
  97. if($this->page>1)
  98. {
  99. $firstPage = "<a href="\"".$this-">pageStr."1\">首页</a>";
  100. }
  101. //如果页码>1则显示上一页连接
  102. if($this->page>1)
  103. {
  104. $previousPage = "<a href="\"".$this-">pageStr.($this->page-1)."\">上一页</a>";
  105. }
  106. //如果没到尾页则显示下一页连接
  107. if($this->page<$this->pageCount)
  108. {
  109. $nextPage = "<a href="\"".$this-">pageStr.($this->page+1)."\">下一页</a>";
  110. }
  111. //如果没到尾页则显示尾页连接
  112. if($this->page<$this->pageCount)
  113. {
  114. $lastPage = "<a href="\"".$this-">pageStr.$this->pageCount."\">尾页</a>";
  115. }
  116. //所有页码列表
  117. for($counter=1;$counter<=$this->pageCount;$counter++)
  118. {
  119. if($this->page == $counter)
  120. {
  121. $currentPage = "<b>".$counter."</b>";
  122. }
  123. else
  124. {
  125. $currentPage = " "."<a href="\"".$this-">pageStr.$counter."\">".$counter."</a>"." ";
  126. }
  127. $pageList .= $currentPage;
  128. //phpfensi.com
  129. }
  130. return $firstPage." ".$previousPage." ".$pageList." ".$nextPage." ".$lastPage." ";
  131. }
  132. }
  133. ?>

用法示例:

  1. <?php
  2. @$db = mysql_connect('localhost', 'root', '123456') or
  3. die("Could not connect to database.");//连接数据库
  4. mysql_query("set names 'utf8'");//输出中文
  5. mysql_select_db('test'); //选择数据库
  6. $sql = "select * from `users`"; //一个简单的查询
  7. $page = new Page('',$sql,$_GET['page'],5,"?page=");
  8. $rows = $page->list;
  9. foreach($rows as $row)
  10. {
  11. echo $row['UserName']."<br>";
  12. }
  13. echo $page->getPageList(); //输出分页列表
  14. ?>