php中分页原与与分页类

所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数: 每页多少条记录($PageSize)?

当前是第几页($CurrentPageID)?

现在只要再给我一个结果集,我就可以显示某段特定的结果出来.

至于其他的参数,比如:上一页($PReviousPageID)、下一页($NextPageID)、总页数($numPages)等等,都可以根据前边这几个东西得到.

以MySQL数据库为例,如果要从表内截取某段内容,sql语句可以用:select * from table limit offset, rows.看看下面一组sql语句,尝试一下发现其中的规率.

前10条记录:select * from table limit 0,10

第11至20条记录:select * from table limit 10,10

第21至30条记录:select * from table limit 20,10

……

这一组sql语句其实就是当$PageSize=10的时候取表内每一页数据的sql语句,我们可以总结出这样一个模板:

select * from table limit ($CurrentPageID – 1) * $PageSize, $PageSize

拿这个模板代入对应的值和上边那一组sql语句对照一下看看是不是那么回事.搞定了最重要的如何获取数据的问题以后,剩下的就仅仅是传递参数,构造合适的sql语句然后使用php从数据库内获取数据并显示了.以下我将用具体代码加以说明.

3、简单代码

请详细阅读以下代码,自己调试运行一次,最好把它修改一次,加上自己的功能,比如搜索等等.

实例代码如下:

  1. <?php
  2. // 建立数据库连接
  3. $link = mysql_connect(“localhost”, “mysql_user”, “mysql_passWord”)
  4. or die(“Could not connect: ” . mysql_error());
  5. // 获取当前页数
  6. if( isset($_GET['page']) ){
  7. $page = intval( $_GET['page'] );
  8. }
  9. else{
  10. $page = 1;
  11. }
  12. // 每页数量
  13. $PageSize = 10;
  14. // 获取总数据量
  15. $sql = “select count(*) as amount from table”;
  16. $result = mysql_query($sql);
  17. $row = mysql_fetch_row($result);
  18. $amount = $row['amount'];
  19. // 记算总共有多少页
  20. if( $amount ){
  21. if( $amount < $page_size ){ $page_count = 1; } //如果总数据量小于$PageSize,那么只有一页
  22. if( $amount % $page_size ){ //取总数据量除以每页数的余数
  23. $page_count = (int)($amount / $page_size) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一
  24. }else{
  25. $page_count = $amount / $page_size; //如果没有余数,则页数等于总数据量除以每页数的结果
  26. }
  27. }
  28. else{
  29. $page_count = 0;
  30. }
  31. // 翻页链接
  32. $page_string = ”;
  33. if( $page == 1 ){
  34. $page_string .= ‘第一页|上一页|’;
  35. }
  36. else{
  37. $page_string .= ‘<a href=?page=1>第一页</a>|<a href=?page=’.($page-1).’>上一页</a>|’;
  38. }
  39. if( ($page == $page_count) || ($page_count == 0) ){
  40. $page_string .= ‘下一页|尾页’;
  41. }
  42. else{
  43. $page_string .= ‘<a href=?page=’.($page+1).’>下一页</a>|<a href=?page=’.$page_count.’>尾页</a>’;
  44. }
  45. // 获取数据,以二维数组格式返回结果
  46. if( $amount ){
  47. $sql = “select * from table order by id desc limit “. ($page-1)*$page_size .”, $page_size”;
  48. $result = mysql_query($sql);
  49. while ( $row = mysql_fetch_row($result) ){
  50. $rowset[] = $row;
  51. }
  52. }else{
  53. $rowset = array();
  54. }
  55. // 没有包含显示结果的代码,那不在讨论范围,只要用foreach就可以很简单的用得到的二维数组来显示结果
  56. ?>

PHP分页器制作,自动生成分面页码,JS调用函数

实例代码如下:

  1. <?php
  2. class PageView{
  3. /**页码**/
  4. public $pageNo = 1;
  5. /**页大小**/
  6. public $pageSize = 20;
  7. /**共多少页**/
  8. public $pageCount = 0;
  9. /**总记录数**/
  10. public $totalNum = 0;
  11. /**偏移量,当前页起始行**/
  12. public $offSet = 0;
  13. /**每页数据**/
  14. public $pageData = array();
  15. /**是否有上一页**/
  16. public $hasPrePage = true;
  17. /**是否有下一页**/
  18. public $hasNextPage = true;
  19. public $pageNoList = array();
  20. public $jsFunction ='jsFunction';
  21. /**
  22. *
  23. * @param unknown_type $count 总行数
  24. * @param unknown_type $size 分页大小
  25. * @param unknown_type $string
  26. */
  27. public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){
  28. $this->totalNum = $count;//总记录数
  29. $this->pageSize = $size;//每页大小
  30. $this->pageNo = $pageNo;
  31. //计算总页数
  32. $this->pageCount = ceil($this->totalNum/$this->pageSize);
  33. $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
  34. //检查pageNo
  35. $this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;
  36. $this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;
  37. //计算偏移
  38. $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
  39. //计算是否有上一页下一页
  40. $this->hasPrePage = $this->pageNo == 1 ?false:true;
  41. $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;
  42. $this->pageData = $pageData;
  43. $this->jsFunction = $jsFunction;
  44. }
  45. /**
  46. * 分页算法
  47. * @return
  48. */
  49. private function generatePageList(){
  50. $pageList = array();
  51. if($this->pageCount <= 9){
  52. for($i=0;$i<$this->pageCount;$i++){
  53. array_push($pageList,$i+1);
  54. }
  55. }else{
  56. if($this->pageNo <= 4){
  57. for($i=0;$i<5;$i++){
  58. array_push($pageList,$i+1);
  59. }
  60. array_push($pageList,-1);
  61. array_push($pageList,$this->pageCount);
  62. }else if($this->pageNo > $this->pageCount - 4){
  63. array_push($pageList,1);
  64. array_push($pageList,-1);
  65. for($i=5;$i>0;$i--){
  66. array_push($pageList,$this->pageCount - $i+1);
  67. }
  68. }else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){
  69. array_push($pageList,1);
  70. array_push($pageList,-1);
  71. array_push($pageList,$this->pageNo -2);
  72. array_push($pageList,$this->pageNo -1);
  73. array_push($pageList,$this->pageNo);
  74. array_push($pageList,$this->pageNo + 1);
  75. array_push($pageList,$this->pageNo + 2);
  76. array_push($pageList,-1);
  77. array_push($pageList,$this->pageCount);
  78. }
  79. }
  80. return $pageList;
  81. }
  82. /***
  83. * 创建分页控件
  84. * @param
  85. * @return String
  86. */
  87. public function echoPageAsDiv(){
  88. $pageList = $this->generatePageList();
  89. $pageString ="<div class='pagination'><div class='page-bottom'>";
  90. if(!emptyempty($pageList)){
  91. if($this->pageCount >1){
  92. if($this->hasPrePage){
  93. $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一页</a>";
  94. }
  95. foreach ($pageList as $k=>$p){
  96. if($this->pageNo == $p){
  97. $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
  98. continue;
  99. }
  100. if($p == -1){
  101. $pageString = $pageString ."<span class='page-break'>...</span>";
  102. continue;
  103. }
  104. $pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>";
  105. }
  106. if($this->hasNextPage){
  107. $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一页</a>";
  108. }
  109. }
  110. }
  111. $pageString = $pageString .("</div></div>");
  112. return $pageString;
  113. }
  114. }
  115. ?>
  116. <style type="text/css">
  117. <!--
  118. .pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
  119. .pagination-tab { margin-bottom: 20px;}
  120. .pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip {
  121. display: inline-block;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; vertical-align: top; white-space: nowrap;}
  122. .pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
  123. border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
  124. .pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
  125. .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
  126. .pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
  127. .pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
  128. .pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
  129. .pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
  130. .pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
  131. .pagination .page-break {border: medium none; background:none transparent; color:#333;}
  132. -->
  133. </style>

PHP调用实例代码如下:

  1. $pageNo = $_GET['pageNo'];
  2. if(emptyempty($pageNo)){
  3. }
  4. //分页数据
  5. $pageData = News::getNewsPage($pageNo,$pageSize);
  6. //取得总行数
  7. $count = News::getNewsCount();
  8. //创建分页器
  9. $p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
  10.      //生成页码
  11. $pageViewString = $p->echoPageAsDiv();