Heim  >  Artikel  >  Backend-Entwicklung  >  带多种分页方式的php分页类

带多种分页方式的php分页类

WBOY
WBOYOriginal
2016-07-25 09:00:34719Durchsuche
  1. Class PAGE {

  2. //类开始
  3. /********************************************************
  4. * $total 记录总数
  5. * $pageNum 每页显示的条数
  6. * $url = '' 链接
  7. * $page->StartPage(显示分类统计,字符分类/数字分页,跳转);
  8. * $page->StartPage(true/false, true/false, true/false);
  9. * site http://bbs.it-home.org
  10. *********************************************************/
  11. private $total; //记录总数
  12. private $pageNum; //每页显示数
  13. private $page; //当前页数
  14. private $pages; //总的页数
  15. private $url; //页面url
  16. private $Aque; //URL参数
  17. /* 构造函数 */
  18. public function PAGE($total, $pageNum, $url='?'){
  19. $this->total = $total; //总记录数.
  20. $this->pageNum = $pageNum; //每页显示数.
  21. $this->url = $this->StrSift($url); //判断$url的值是否合法.
  22. $this->Aque = $_GET; //页面原来所传递参数.
  23. $this->page = $this->StrSift($_GET['page']); //当前页面GET(全局变量)方式参数,当前页码.
  24. $this->page = is_numeric($this->page) ? $this->page : 1; //当前页码不为数字时,则把其设为1.
  25. $this->pages = ceil($total/$pageNum); //总页数.
  26. if($this->pagepage = 1; //当页码小于1时,则把其设为1.
  27. //if($this->page>$this->pages) $this->page = $this->pages; //当页码大于最大页码时,则把其设为最大页码.
  28. }
  29. /******************
  30. * 分页方法
  31. *******************/
  32. function StartPage($str, $view=true, $jump=true){
  33. if($view==true) $PageStr .= $this->GetCount(); //分页统计信息
  34. if($str=='str') $PageStr .= $this->GetPageStr(); //选择字符分页形式
  35. else $PageStr .= $this->GetPageNum(); //选择数字分页形式
  36. if($jump==true) $PageStr .= $this->JumpSelect(); //跳转
  37. return $PageStr;
  38. }
  39. /**********************************************

  40. * 显示统计信息. 格式:共5条记录 页:2/3
  41. ***********************************************/
  42. function GetCount(){
  43. $CountStr = "共". $this->total ."条记录 页:".$this->page."/".$this->pages."  ";
  44. return $CountStr;
  45. }
  46. /***********************************************

  47. * 分页格式形一:第一页 上一页 下一页 末 页
  48. ************************************************/
  49. function GetPageStr(){
  50. $url = $this->url; //获取URL
  51. //对URL参数进行处理:数组的键是URL变量,数组的值是URL变量的值.
  52. foreach($this->Aque as $key => $val){
  53. switch($key){
  54. case "page":
  55. $Next = $val + 1;
  56. $Prev = $val - 1;
  57. break;
  58. default:
  59. $Sque .= "&$key=".$this->StrSift($val);
  60. }
  61. }
  62. if($Next==0) $Next=2;
  63. //首 页 上一页
  64. switch($this->page){
  65. case $this->page $pagestr .= "首 页  ";
  66. $pagestr .= "上一页  ";
  67. break;
  68. default:
  69. $pagestr .= "首 页  ";
  70. $pagestr .= "上一页  ";
  71. }
  72. //下一页 末 页
  73. switch($this->page){
  74. case $this->page>=$this->pages:
  75. $pagestr .= "下一页  ";
  76. $pagestr .= "末 页  ";
  77. break;
  78. default:
  79. $pagestr .= "下一页  ";
  80. $pagestr .= "末 页  ";
  81. }
  82. //返回分页字符串.
  83. return $pagestr;
  84. }
  85. /***********************************************************

  86. * 分页格式形如:共4307条记录 页:1/72 1 2 3 4 5 6 7 8 9 10
  87. ************************************************************/
  88. function GetPageNum(){
  89. $url = $this->url;
  90. //对URL参数进行处理:数组的键是URL变量,数组的值是URL变量的值.
  91. foreach($this->Aque as $key => $val){
  92. switch($key){
  93. case $key!="page":
  94. $Sque .= "&$key=".$this->StrSift($val);
  95. }
  96. }
  97. switch($this->pages){
  98. //总页数大于12页:
  99. case $this->pages>12:
  100. //分页数字前: switch($this->page){
  101. case $this->page>1:
  102. $pagestr .= " ";
  103. $pagestr .= " ";
  104. break;
  105. default:
  106. $pagestr .= " $pagestr .= " }
  107. //分页数字:1 2 3 4 5 6 当前页码左边6个分页链接,右边6个分页链接.
  108. for($i=$this->page-6; $ipage+6; $i++){
  109. if($i>$this->pages) break;
  110. if($i==$this->page) $pagestr .= $i." ";
  111. elseif($i>=1) $pagestr .= "$i ";
  112. }
  113. //分页数字后: > >>
  114. switch($this->page){
  115. case $this->pagepages:
  116. $pagestr .= ">> ";
  117. $pagestr .= "> ";
  118. break;
  119. default:
  120. $pagestr .= "> ";
  121. $pagestr .= ">> ";
  122. }
  123. break;
  124. default:
  125. //总页数小于12页:
  126. for($i=1; $ipages; $i++){
  127. switch($i){
  128. case $i==$this->page:
  129. $pagestr .= $i." ";
  130. break;
  131. default:
  132. $pagestr .= "$i ";
  133. }
  134. }
  135. }
  136. //返回分页字符串.
  137. return $pagestr;
  138. }
  139. /************************
  140. * 定义跳转页. BEGIN
  141. *************************/
  142. function JumpSelect(){
  143. $url = $this->url;
  144. //对URL参数进行处理:数组的键是URL变量,数组的值是URL变量的值.
  145. foreach($this->Aque as $key => $val){
  146. if($key != "page") $Sque .= "&$key=".$this->StrSift($val);
  147. }
  148. $SelectStr = "\n\n";
  149. //返回分页字符串.
  150. return $SelectStr;
  151. }
  152. /**********************************

  153. * 过滤特殊字符.
  154. ***********************************/
  155. private function StrSift($str){
  156. $str = str_replace("\"","",$str);
  157. $str = str_replace("'","",$str);
  158. $str = str_replace("[url=file://%22,%22%22,$str/]\\","",$str[/url]);
  159. $str = str_replace("\/","",$str);
  160. $str = str_replace(":","",$str);
  161. $str = str_replace("?","",$str); //去除会出现"??".
  162. $str = str_replace(">","",$str);
  163. $str = str_replace(" $str = str_replace("%","",$str);
  164. $str = str_replace("*","",$str);
  165. $str = str_replace("&","",$str);
  166. $str = str_replace(".","",$str);
  167. return $str;
  168. }
  169. //类结束

  170. }
  171. ?>
复制代码

有兴趣的朋友,还可以参考下如下的文章: 一个不错的php分页类的代码 一个实用的php分页类

分页样式表:

复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn