Heim  >  Artikel  >  Backend-Entwicklung  >  实用型php分页类,多种分页样式

实用型php分页类,多种分页样式

WBOY
WBOYOriginal
2016-07-25 08:52:42861Durchsuche
复制代码
  1. class SubPages{
  2. private $each_disNums;//每页显示的条目数
  3. private $nums;//总条目数
  4. private $current_page;//当前被选中的页
  5. private $sub_pages;//每次显示的页数
  6. private $pageNums;//总页数
  7. private $point;//总页数
  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,$point){
  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->point=$point;
  36. $this->show_SubPages($subPage_type);
  37. //echo $this->pageNums."--".$this->sub_pages;
  38. }
  39. / *
  40. __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
  41. * /
  42. function __destruct(){
  43. unset($each_disNums);
  44. unset($nums);
  45. unset($current_page);
  46. unset($sub_pages);
  47. unset($pageNums);
  48. unset($page_array);
  49. unset($subPage_link);
  50. unset($subPage_type);
  51. }
  52. / *
  53. show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
  54. * /
  55. function show_SubPages($subPage_type){
  56. if($subPage_type == 1){
  57. $this->subPageCss1();
  58. }elseif ($subPage_type == 2){
  59. $this->subPageCss2();
  60. }
  61. }
  62. / *
  63. 用来给建立分页的数组初始化的函数。
  64. * /
  65. function initArray(){
  66. for($i=0;$isub_pages;$i++){
  67. $this->page_array[$i]=$i;
  68. }
  69. return $this->page_array;
  70. }
  71. / *
  72. construct_num_Page该函数使用来构造显示的条目
  73. 即使:[1][2][3][4][5][6][7][8][9][10]
  74. * /
  75. function construct_num_Page(){
  76. if($this->pageNums sub_pages){
  77. $current_array=array();
  78. for($i=0;$ipageNums;$i++){
  79. $current_array[$i]=$i+1;
  80. }
  81. }else{
  82. $current_array=$this->initArray();
  83. if($this->current_page for($i=0;$i $current_array[$i]=$i+1;
  84. }
  85. }elseif ($this->current_page pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  86. for($i=0;$i $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  87. }
  88. }else{
  89. for($i=0;$i $current_array[$i]=$this->current_page-2+$i;
  90. }
  91. }
  92. }
  93. return $current_array;
  94. }
  95. / *
  96. 构造经典模式的分页
  97. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  98. * /
  99. function subPageCss2(){
  100. $subPageCss2Str="";
  101. //2462347条商家信息
  102. $subPageCss2Str.= "当前第".$this->current_page." /".$this->pageNums." 页     共".$this->nums."条商家信息";
  103. //$subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  104. if($this->current_page > 1){
  105. $firstPageUrl=$this->subPage_link."1"."#zkfb_shop";
  106. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->point;
  107. //首页上一页
  108. $subPageCss2Str.="首页";
  109. $subPageCss2Str.="上一页";
  110. }else {
  111. $subPageCss2Str.="首页";
  112. $subPageCss2Str.="上一页";
  113. }
  114. $a=$this->construct_num_Page();
  115. for($i=0;$i $s=$a[$i];
  116. if($s == $this->current_page ){
  117. // $subPageCss2Str.="[".$s."]";
  118. $subPageCss2Str.="".$s."";
  119. }else{
  120. $url=$this->subPage_link.$s.$this->point;
  121. // 2
  122. $subPageCss2Str.="".$s."";
  123. // $subPageCss2Str.="[".$s."]";
  124. }
  125. }
  126. if($this->current_page pageNums){
  127. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->point;
  128. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->point;
  129. //下一页末页
  130. $subPageCss2Str.="下一页";
  131. $subPageCss2Str.="尾页
  132. ";
  133. }else {
  134. $subPageCss2Str.="下一页";
  135. $subPageCss2Str.="尾页
  136. ";
  137. }
  138. echo $subPageCss2Str;
  139. }
  140. }
  141. ?>
复制代码


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