二个php分页程序代码

PHP分页器制作,自动生成分面页码,JS调用函数,分页的原理大致如下,分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page).

有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在mysql里如果要想取出表内某段特定内容可以使用的 T-SQL语句:select * from table limit offset,rows来实现。这里的offset是记录偏移量,它的计算方法是offset=$pagesize*($page-1),rows是要显示的记录条数,这里就是$page,也就是说select * from table limit 10,10这条语句的意思是取出表里从第11条记录开始的20条记录.

PHP实例代码如下:

  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. ?>

css,代码如下:

  1. <style type="text/css">
  2. <!--
  3. .pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
  4. .pagination-tab { margin-bottom: 20px;}
  5. .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 {
  6. 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;}
  7. .pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
  8. border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
  9. .pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
  10. .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
  11. .pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
  12. .pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
  13. .pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
  14. .pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
  15. .pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
  16. .pagination .page-break {border: medium none; background:none transparent; color:#333;}
  17. -->
  18. </style>

使用方法,代码如下:

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

下面再介绍一个分页类,代码如下:

  1. <?php
  2. class SubPages{
  3. private $each_disNums;//每页显示的条目数
  4. private $nums;//总条目数
  5. private $current_page;//当前被选中的页
  6. private $sub_pages;//每次显示的页数
  7. private $pageNums;//总页数
  8. private $page_array = array();//用来构造分页的数组
  9. private $subPage_link;//每个分页的链接
  10. private $subPage_type;//显示分页的类型
  11. /*
  12. __construct是SubPages的构造函数,用来在创建类的时候自动运行.
  13. @$each_disNums 每页显示的条目数
  14. @nums 总条目数
  15. @current_num 当前被选中的页
  16. @sub_pages 每次显示的页数
  17. @subPage_link 每个分页的链接
  18. @subPage_type 显示分页的类型
  19. 当@subPage_type=1的时候为普通分页模式
  20. example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  21. 当@subPage_type=2的时候为经典分页样式
  22. example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  23. */
  24. function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){
  25. $this->each_disNums=intval($each_disNums);
  26. $this->nums=intval($nums);
  27. if(!$current_page){
  28. $this->current_page=1;
  29. }else{
  30. $this->current_page=intval($current_page);
  31. }
  32. $this->sub_pages=intval($sub_pages);
  33. $this->pageNums=ceil($nums/$each_disNums);
  34. $this->subPage_link=$subPage_link;
  35. $this->show_SubPages($subPage_type);
  36. //echo $this->pageNums."--".$this->sub_pages;
  37. }
  38. /*
  39. __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
  40. */
  41. function __destruct(){
  42. unset($each_disNums);
  43. unset($nums);
  44. unset($current_page);
  45. unset($sub_pages);
  46. unset($pageNums);
  47. unset($page_array);
  48. unset($subPage_link);
  49. unset($subPage_type);
  50. }
  51. /*
  52. show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
  53. */
  54. function show_SubPages($subPage_type){
  55. if($subPage_type == 1){
  56. $this->subPageCss1();
  57. }elseif ($subPage_type == 2){
  58. $this->subPageCss2();
  59. }
  60. }
  61. /*
  62. 用来给建立分页的数组初始化的函数。
  63. */
  64. function initArray(){
  65. for($i=0;$i<$this->sub_pages;$i++){
  66. $this->page_array[$i]=$i;
  67. }
  68. return $this->page_array;
  69. }
  70. /*
  71. construct_num_Page该函数使用来构造显示的条目
  72. 即使:[1][2][3][4][5][6][7][8][9][10]
  73. */
  74. function construct_num_Page(){
  75. if($this->pageNums < $this->sub_pages){
  76. $current_array=array();
  77. for($i=0;$i<$this->pageNums;$i++){
  78. $current_array[$i]=$i+1;
  79. }
  80. }else{
  81. $current_array=$this->initArray();
  82. if($this->current_page <= 3){
  83. for($i=0;$i<count($current_array);$i++){
  84. $current_array[$i]=$i+1;
  85. }
  86. }elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  87. for($i=0;$i<count($current_array);$i++){
  88. $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  89. }
  90. }else{
  91. for($i=0;$i<count($current_array);$i++){
  92. $current_array[$i]=$this->current_page-2+$i;
  93. }
  94. }
  95. }
  96. return $current_array;
  97. }
  98. /*
  99. 构造普通模式的分页
  100. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  101. */
  102. function subPageCss1(){
  103. $subPageCss1Str="";
  104. $subPageCss1Str.="共".$this->nums."条记录,";
  105. $subPageCss1Str.="每页显示".$this->each_disNums."条,";
  106. $subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  107. if($this->current_page > 1){
  108. $firstPageUrl=$this->subPage_link."1";
  109. $prewPageUrl=$this->subPage_link.($this->current_page-1);
  110. $subPageCss1Str.="[<a href='$firstPageUrl'>首页</a>] ";
  111. $subPageCss1Str.="[<a href='$prewPageUrl'>上一页</a>] ";
  112. }else {
  113. $subPageCss1Str.="[首页] ";
  114. $subPageCss1Str.="[上一页] ";
  115. }
  116. if($this->current_page < $this->pageNums){
  117. $lastPageUrl=$this->subPage_link.$this->pageNums;
  118. $nextPageUrl=$this->subPage_link.($this->current_page+1);
  119. $subPageCss1Str.=" [<a href='$nextPageUrl'>下一页</a>] ";
  120. $subPageCss1Str.="[<a href='$lastPageUrl'>尾页</a>] ";
  121. }else {
  122. $subPageCss1Str.="[下一页] ";
  123. $subPageCss1Str.="[尾页] ";
  124. }
  125. echo $subPageCss1Str;
  126. }
  127. /*
  128. 构造经典模式的分页
  129. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  130. */
  131. function subPageCss2(){
  132. $subPageCss2Str="";
  133. $subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  134. if($this->current_page > 1){
  135. $firstPageUrl=$this->subPage_link."1";
  136. $prewPageUrl=$this->subPage_link.($this->current_page-1);
  137. $subPageCss2Str.="[<a href='$firstPageUrl'>首页</a>] ";
  138. $subPageCss2Str.="[<a href='$prewPageUrl'>上一页</a>] ";
  139. }else {
  140. $subPageCss2Str.="[首页] ";
  141. $subPageCss2Str.="[上一页] ";
  142. }
  143. $a=$this->construct_num_Page();
  144. for($i=0;$i<count($a);$i++){
  145. $s=$a[$i];
  146. if($s == $this->current_page ){
  147. $subPageCss2Str.="[<span >".$s."</span>]";
  148. }else{
  149. $url=$this->subPage_link.$s;
  150. $subPageCss2Str.="[<a href='$url'>".$s."</a>]";
  151. }
  152. }
  153. if($this->current_page < $this->pageNums){
  154. $lastPageUrl=$this->subPage_link.$this->pageNums;
  155. $nextPageUrl=$this->subPage_link.($this->current_page+1);
  156. $subPageCss2Str.=" [<a href='$nextPageUrl'>下一页</a>] ";
  157. $subPageCss2Str.="[<a href='$lastPageUrl'>尾页</a>] ";
  158. }else {
  159. $subPageCss2Str.="[下一页] ";
  160. $subPageCss2Str.="[尾页] ";
  161. }
  162. echo $subPageCss2Str;
  163. }
  164. }
  165. ?>

调用方法,代码如下:

  1. <?php
  2. require_once("SubPages.php");
  3. //每页显示的条数
  4. $page_size=20;
  5. //总条目数
  6. $nums=1024;
  7. //每次显示的页数
  8. $sub_pages=10;
  9. //得到当前是第几页
  10. $pageCurrent=$_GET["p"];
  11. //if(!$pageCurrent) $pageCurrent=1;
  12. $subPages=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,"test.php?p=",2);
  13. ?>