Home >Backend Development >PHP Tutorial >PHP paging code, PHP paging code with beautiful styles

PHP paging code, PHP paging code with beautiful styles

WBOY
WBOYOriginal
2016-07-25 08:52:461202browse
  1. include "pager.class.php";
  2. $CurrentPage=isset($_GET['page'])?$_GET['page']:1;
  3. //die($CurrentPage);
  4. $myPage=new pager(1300,intval($CurrentPage));
  5. $pageStr= $myPage->GetPagerContent();
  6. //echo $pageStr;
  7. $myPage=new pager(90,intval($CurrentPage));
  8. $pageStr= $myPage->GetPagerContent();
  9. echo $pageStr;
  10. ?>
复制代码
  1. php分页演示-bbs.it-home.org
  2. include "pager.class.php";
  3. $CurrentPage=isset($_GET['page'])?$_GET['page']:1;
  4. //die($CurrentPage);
  5. $myPage=new pager(1300,intval($CurrentPage));
  6. $pageStr= $myPage->GetPagerContent();
  7. //echo $pageStr;
  8. $myPage=new pager(90,intval($CurrentPage));
  9. $pageStr= $myPage->GetPagerContent();
  10. echo $pageStr;
  11. ?>
复制代码

2) pager.class.php:

  1. /*

  2. * PHP分页类
  3. * @package Page
  4. * @Created 2013-03-27
  5. * @Modify 2013-03-27
  6. * Example:
  7. $myPage=new Pager(1300,intval($CurrentPage));
  8. $pageStr= $myPage->GetPagerContent();
  9. echo $pageStr;
  10. */
  11. class Pager {
  12. private $pageSize = 10;
  13. private $pageIndex;
  14. private $totalNum;

  15. private $totalPagesCount;

  16. private $pageUrl;

  17. private static $_instance;

  18. public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 10,$p_initNum=3,$p_initMaxNum=5) {

  19. if (! isset ( $p_totalNum ) || !isset($p_pageIndex)) {
  20. die ( "pager initial error" );
  21. }

  22. $this->totalNum = $p_totalNum;

  23. $this->pageIndex = $p_pageIndex;
  24. $this->pageSize = $p_pageSize;
  25. $this->initNum=$p_initNum;
  26. $this->initMaxNum=$p_initMaxNum;
  27. $this->totalPagesCount= ceil($p_totalNum / $p_pageSize);
  28. $this->pageUrl=$this->_getPageUrl();

  29. $this->_initPagerLegal();

  30. }

  31. /**
  32. * Get the current URL string with the page part removed
  33. *
  34. * @return String URL string
  35. */
  36. private function _getPageUrl() {
  37. $CurrentUrl = $_SERVER["REQUEST_URI"];
  38. $arrUrl = parse_url($CurrentUrl);
  39. $urlQuery = $arrUrl["query"];

  40. if($urlQuery){

  41. $urlQuery = ereg_replace("(^|&)page=" . $this->pageIndex, "", $urlQuery);
  42. $CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl);

  43. if($urlQuery){

  44. $CurrentUrl.="&page";
  45. }
  46. else $CurrentUrl.="page";

  47. } else {

  48. $CurrentUrl.="?page";
  49. }

  50. return $CurrentUrl;

  51. }

  52. /*
  53. *设置页面参数合法性
  54. *@return void
  55. */
  56. private function _initPagerLegal()
  57. {
  58. if((!is_numeric($this->pageIndex)) || $this->pageIndex<1)
  59. {
  60. $this->pageIndex=1;
  61. }elseif($this->pageIndex > $this->totalPagesCount)
  62. {
  63. $this->pageIndex=$this->totalPagesCount;
  64. }

  65. }

  66. //$this->pageUrl}={$i}
  67. //{$this->CurrentUrl}={$this->TotalPages}
  68. public function GetPagerContent() {
  69. $str = "
  70. //首页 上一页
  71. if($this->pageIndex==1)
  72. {
  73. $str .="首页 "."n";
  74. $str .="上一页 "."n"."n";
  75. }else
  76. {
  77. $str .="首页 "."n";
  78. $str .="上一页 "."n"."n";
  79. }
  80. /*

  81. 除首末后 页面分页逻辑

  82. */
  83. //10页(含)以下
  84. $currnt="";
  85. if($this->totalPagesCount<=10)
  86. {

  87. for($i=1;$i<=$this->totalPagesCount;$i++)

  88. {

  89. if($i==$this->pageIndex)
  90. { $currnt=" class='current'";}
  91. else
  92. { $currnt=""; }
  93. $str .="$i"."n" ;
  94. }
  95. }else //10页以上
  96. { if($this->pageIndex<3) //当前页小于3
  97. {
  98. for($i=1;$i<=3;$i++)
  99. {
  100. if($i==$this->pageIndex)
  101. { $currnt=" class='current'";}
  102. else
  103. { $currnt=""; }
  104. $str .="$i"."n" ;
  105. }

  106. $str.="……"."n";

  107. for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1

  108. {
  109. $str .="$i"."n" ;

  110. }

  111. }elseif($this->pageIndex<=5) // 5 >= 当前页 >= 3
  112. {
  113. for($i=1;$i<=($this->pageIndex+1);$i++)
  114. {
  115. if($i==$this->pageIndex)
  116. { $currnt=" class='current'";}
  117. else
  118. { $currnt=""; }
  119. $str .="$i"."n" ;

  120. }

  121. $str.="……"."n";

  122. for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1

  123. {
  124. $str .="$i"."n" ;
  125. }

  126. }elseif(5<$this->pageIndex && $this->pageIndex<=$this->totalPagesCount-5 ) //当前页大于5,同时小于总页数-5

  127. {

  128. for($i=1;$i<=3;$i++)

  129. {
  130. $str .="$i"."n" ;
  131. }
  132. $str.="……";
  133. for($i=$this->pageIndex-1 ;$i<=$this->pageIndex+1 && $i<=$this->totalPagesCount-5+1;$i++)
  134. {
  135. if($i==$this->pageIndex)
  136. { $currnt=" class='current'";}
  137. else
  138. { $currnt=""; }
  139. $str .="$i"."n" ;
  140. }
  141. $str.="……";

  142. for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)

  143. {
  144. $str .="$i"."n" ;

  145. }

  146. }else
  147. {

  148. for($i=1;$i<=3;$i++)

  149. {
  150. $str .="$i"."n" ;
  151. }
  152. $str.="……"."n";

  153. for($i=$this->totalPagesCount-5;$i<=$this->totalPagesCount;$i++)//功能1

  154. {
  155. if($i==$this->pageIndex)
  156. { $currnt=" class='current'";}
  157. else
  158. { $currnt=""; }
  159. $str .="$i"."n" ;

  160. }

  161. }
  162. }

  163. /*

  164. 除首末后 页面分页逻辑结束
  165. */
  166. //下一页 末页
  167. if($this->pageIndex==$this->totalPagesCount)
  168. {
  169. $str .="n"."下一页"."n" ;
  170. $str .="末页"."n";

  171. }else
  172. {
  173. $str .="n"."下一页 "."n";
  174. $str .="末页 "."n" ;
  175. }

  176. $str .= "

";
  • return $str;
  • }

  • /**

  • * Get instance
  • * @return
  • */
  • // static public function getInstance() {
  • // if (is_null ( self::$_instance )) {
  • // self::$_instance = new pager ();
  • // }
  • // return self::$_instance;
  • // }

  • }
  • ?>

  • 复制代码

    3),php分页样式文件 pager.css:

    1. body,html{ padding:0px; margin:0px; color:#333333; font-family:"宋体",Arial,Lucida,Verdana,Helvetica,sans-serif; font-size:12px; line-height:150%;}

    2. h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label{margin:0; padding:0; border:none; list-style-type:none;}

    3. /**Front page pagination style**/

    4. .Pagination {margin:10px 0 0;padding:5px 0;text-align:rightright; height:20px; line-height:20px; font-family:Arial, Helvetica, sans-serif,"宋体";}
    5. .Pagination a {margin-left:2px;padding:2px 7px 2px;}
    6. .Pagination .dot{ border:medium none; padding:4px 8px}
    7. .Pagination a:link, .Pagination a:visited {border:1px solid #dedede;color:#696969;text-decoration:none;}
    8. .Pagination a:hover, .Pagination a:active, .Pagination a.current:link, .Pagination a.current:visited {border:1px solid #dedede;color:#fff; background-color:#ff6600; background-image:none; border:#ff6600 solid 1px;}
    9. .Pagination .selectBar{ border:#dedede solid 1px; font-size:12px; width:95px; height:21px; line-height:21px; margin-left:10px; display:inline}
    10. .Pagination a.tips{_padding:4px 7px 1px;}

    复制代码


    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn