Home  >  Article  >  Backend Development  >  Practical php paging class, multiple paging styles

Practical php paging class, multiple paging styles

WBOY
WBOYOriginal
2016-07-25 08:52:42907browse
复制代码
  1. class SubPages{
  2. private $each_disNums; //The number of entries displayed on each page
  3. private $nums; //Total number of entries
  4. private $current_page; //The currently selected page
  5. private $sub_pages;//Number of pages displayed each time
  6. private $pageNums;//Total number of pages
  7. private $point;//Total number of pages
  8. private $page_array = array();//Array used to construct paging
  9. private $subPage_link;//The link of each page
  10. private $subPage_type;//Display the type of page
  11. / *
  12. __construct is the constructor of SubPages, which is used to run automatically when creating the class.
  13. @$each_disNums Displayed on each page Number of entries
  14. @nums Total number of entries
  15. @current_num Currently selected page
  16. @sub_pages Number of pages displayed each time
  17. @subPage_link Link to each page
  18. @subPage_type Display type of paging
  19. When @subPage_type=1 Normal paging mode
  20. example: 4523 records in total, 10 records displayed on each page, current page 1/453 [Home] [Previous page] [Next page] [Last page]
  21. When @subPage_type=2, it is the classic paging style
  22. example: Current page 1/453 [Home page] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page]
  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 destructor, called when the class is no longer in use, this function is used to release resources.
  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 function is used in the constructor. And used to determine what kind of paging to display
  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. Function used to initialize the array for creating paging.
  64. * /
  65. function initArray(){
  66. for($i=0;$i<$this->sub_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 < $this->sub_pages){
  77. $current_array=array();
  78. for($i=0;$i<$this->pageNums;$i++){
  79. $current_array[$i]=$i+1;
  80. }
  81. }else{
  82. $current_array=$this->initArray();
  83. if($this->current_page <= 3){
  84. for($i=0;$i $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 $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  88. }
  89. }else{
  90. for($i=0;$i $current_array[$i]=$this->current_page-2+$i;
  91. }
  92. }
  93. }
  94. return $current_array;
  95. }
  96. / *
  97. 构造经典模式的分页
  98. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  99. * /
  100. function subPageCss2(){
  101. $subPageCss2Str="";
  102. //2462347条商家信息
  103. $subPageCss2Str.= "当前第".$this->current_page." /".$this->pageNums." 页     共".$this->nums."条商家信息";
  104. //$subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  105. if($this->current_page > 1){
  106. $firstPageUrl=$this->subPage_link."1"."#zkfb_shop";
  107. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->point;
  108. //首页上一页
  109. $subPageCss2Str.="首页";
  110. $subPageCss2Str.="上一页";
  111. }else {
  112. $subPageCss2Str.="首页";
  113. $subPageCss2Str.="上一页";
  114. }
  115. $a=$this->construct_num_Page();
  116. for($i=0;$i $s=$a[$i];
  117. if($s == $this->current_page ){
  118. // $subPageCss2Str.="[".$s."]";
  119. $subPageCss2Str.="".$s."";
  120. }else{
  121. $url=$this->subPage_link.$s.$this->point;
  122. // 2
  123. $subPageCss2Str.="".$s."";
  124. // $subPageCss2Str.="[".$s."]";
  125. }
  126. }
  127. if($this->current_page < $this->pageNums){
  128. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->point;
  129. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->point;
  130. //下一页末页
  131. $subPageCss2Str.="下一页";
  132. $subPageCss2Str.="尾页";
  133. }else {
  134. $subPageCss2Str.="下一页";
  135. $subPageCss2Str.="尾页";
  136. }
  137. echo $subPageCss2Str;
  138. }
  139. }
  140. ?>
复制代码


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