A practical php paging class

WBOY
Release: 2016-07-25 09:11:04
Original
858 people have browsed it
  1. /*

  2. Practical php paging class
  3. subpages
  4. */
  5. class SubPages{
  6. private $each_disNums;//The number of entries displayed per page
  7. private $nums ;//Total number of entries
  8. private $current_page;//The currently selected page
  9. private $sub_pages;//Number of pages displayed each time
  10. private $pageNums;//Total number of pages
  11. private $page_array = array(); //Array used to construct pagination
  12. private $subPage_link;//Links for each pagination
  13. private $subPage_type;//Display paging type

  14. /*

  15. __construct is the constructor of SubPages , used to run automatically when creating a class.
  16. @$each_disNums The number of entries displayed on each page
  17. @nums The total number of entries
  18. @current_num The currently selected page
  19. @sub_pages The number of pages displayed each time
  20. @subPage_link Each page The link
  21. @subPage_type displays the type of paging
  22. When @subPage_type=1, it is the normal paging mode
  23. example: A total of 4523 records, 10 records are displayed on each page, the current page 1/453 [Home] [Previous page] [ Next page] [Last page]
  24. When @subPage_type=2, it is the classic paging style
  25. example: Current page 1/453 [Home page] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [ Last page]
  26. */
  27. function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){
  28. $this->each_disNums=intval($each_disNums);
  29. $this->nums =intval($nums);
  30. if(!$current_page){
  31. $this->current_page=1;
  32. }else{
  33. $this->current_page=intval($current_page);
  34. }
  35. $this-> ;sub_pages=intval($sub_pages);
  36. $this->pageNums=ceil($nums/$each_disNums);
  37. $this->subPage_link=$subPage_link;
  38. $this->show_SubPages($subPage_type);
  39. //echo $this->pageNums."--".$this->sub_pages;
  40. }
  41. /*
  42. __destruct destructor, called when the class is no longer in use, this function is used to release resources.
  43. */
  44. function __destruct(){
  45. unset($each_disNums);
  46. unset($nums);
  47. unset($current_page);
  48. unset($sub_pages);
  49. unset($pageNums);
  50. unset($page_array) ;
  51. unset($subPage_link);
  52. unset($subPage_type);
  53. }
  54. /*
  55. show_SubPages function is used in the constructor. And used to determine what kind of paging to display
  56. */
  57. function show_SubPages($subPage_type){
  58. if($subPage_type == 1){
  59. $this->subPageCss1();
  60. }elseif ($subPage_type == 2) {
  61. $this->subPageCss2();
  62. }
  63. }
  64. /*
  65. Function used to initialize the array for creating pagination.
  66. */
  67. function initArray(){
  68. for($i=0;$i<$this->sub_pages;$i++){
  69. $this->page_array[$i]=$i;
  70. }
  71. return $this->page_array;
  72. }
  73. /*
  74. construct_num_Page该函数使用来构造显示的条目
  75. 即使:[1][2][3][4][5][6][7][8][9][10]
  76. */
  77. function construct_num_Page(){
  78. if($this->pageNums < $this->sub_pages){
  79. $current_array=array();
  80. for($i=0;$i<$this->pageNums;$i++){
  81. $current_array[$i]=$i+1;
  82. }
  83. }else{
  84. $current_array=$this->initArray();
  85. if($this->current_page <= 3){
  86. for($i=0;$i $current_array[$i]=$i+1;
  87. }
  88. }elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  89. for($i=0;$i $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  90. }
  91. }else{
  92. for($i=0;$i $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.="[首页] ";
  111. $subPageCss1Str.="[上一页] ";
  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.=" [下一页] ";
  120. $subPageCss1Str.="[尾页] ";
  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.="[首页] ";
  138. $subPageCss2Str.="[上一页] ";
  139. }else {
  140. $subPageCss2Str.="[首页] ";
  141. $subPageCss2Str.="[上一页] ";
  142. }
  143. $a=$this->construct_num_Page();
  144. for($i=0;$i $s=$a[$i];
  145. if($s == $this->current_page ){
  146. $subPageCss2Str.="[".$s."]";
  147. }else{
  148. $url=$this->subPage_link.$s;
  149. $subPageCss2Str.="[".$s."]";
  150. }
  151. }
  152. if($this->current_page < $this->pageNums){
  153. $lastPageUrl=$this->subPage_link.$this->pageNums;
  154. $nextPageUrl=$this->subPage_link.($this->current_page+1);
  155. $subPageCss2Str.=" [下一页] ";
  156. $subPageCss2Str.="[尾页] ";
  157. }else {
  158. $subPageCss2Str.="[下一页] ";
  159. $subPageCss2Str.="[尾页] ";
  160. }
  161. echo $subPageCss2Str;
  162. }
  163. }
  164. ?>

复制代码

Test page, test.php

  1. require_once("SubPages.php");
  2. //Number of items displayed per page

  3. $page_size=20;

  4. //Total number of entries

  5. $nums=1024;

  6. //Number of pages displayed each time

  7. $sub_pages=10;

  8. //Get the current Which page

  9. $pageCurrent=$_GET["p"];

  10. //if(!$pageCurrent) $pageCurrent=1;
  11. $subPages=new SubPages($page_size,$ nums,$pageCurrent,$sub_pages,"test.php?p=",2);
  12. ?>

Copy code


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!