search
HomeBackend DevelopmentPHP TutorialPHP paging class code, supports pseudo-static PHP paging class

  1. class Page{
  2. protected $each_disNums;//The number of entries displayed on each page
  3. protected $nums;//The total number of entries
  4. protected $current_page;//The currently selected page
  5. protected $sub_pages;//The number of pages displayed each time
  6. protected $pageNums;//Total number of pages
  7. protected $page_array = array();//Array used to construct paging
  8. protected $subPage_link;//Each paging Link
  9. protected $subPage_type;//Display the type of paging
  10. protected $houz;//Suffix
  11. /*
  12. __construct is the constructor of SubPages, which is used to run automatically when creating a class.
  13. @$each_disNums Entries displayed on each page Number
  14. @nums The total number of entries
  15. @current_num The currently selected page
  16. @sub_pagesThe number of pages displayed each time
  17. @subPage_linkThe link of each page
  18. @subPage_type displays the type of paging
  19. When @subPage_type=1, it is normal Pagination 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,$houz=''){
  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->show_SubPages($subPage_type);
  36. $this->houz=$houz;
  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 paging array.
  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. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  98. */ bbs.it-home.org
  99. function subPageCss1(){
  100. $subPageCss1Str="";
  101. $subPageCss1Str.="共".$this->nums."条记录,";
  102. $subPageCss1Str.="每页显示".$this->each_disNums."条,";
  103. $subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  104. if($this->current_page > 1){
  105. $firstPageUrl=$this->subPage_link."1".$this->houz;
  106. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->houz;
  107. $subPageCss1Str.="[首页] ";
  108. $subPageCss1Str.="[上一页] ";
  109. }else {
  110. $subPageCss1Str.="[首页] ";
  111. $subPageCss1Str.="[上一页] ";
  112. }
  113. if($this->current_page pageNums){
  114. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->houz;
  115. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->houz;
  116. $subPageCss1Str.=" [下一页] ";
  117. $subPageCss1Str.="[尾页] ";
  118. }else {
  119. $subPageCss1Str.="[下一页] ";
  120. $subPageCss1Str.="[尾页] ";
  121. }
  122. return $subPageCss1Str;
  123. }
  124. /*
  125. 构造经典模式的分页
  126. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  127. */
  128. function subPageCss2(){
  129. $subPageCss2Str="";
  130. $subPageCss2Str.="共[".$this->nums."]条 当前第".$this->current_page."/".$this->pageNums."页";
  131. if($this->current_page > 1){
  132. $firstPageUrl=$this->subPage_link."1".$this->houz;
  133. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->houz;
  134. $subPageCss2Str.="[首页] ";
  135. $subPageCss2Str.="[上一页] ";
  136. }else {
  137. $subPageCss2Str.="[首页] ";
  138. $subPageCss2Str.="[上一页] ";
  139. }
  140. $a=$this->construct_num_Page();
  141. for($i=0;$i$s=$a[$i];
  142. if($s == $this->current_page ){
  143. $subPageCss2Str.="[".$s."]";
  144. }else{
  145. $url=$this->subPage_link.$s.$this->houz;
  146. $subPageCss2Str.="[".$s."]";
  147. }
  148. }
  149. if($this->current_page pageNums){
  150. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->houz;
  151. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->houz;
  152. $subPageCss2Str.=" [下一页] ";
  153. $subPageCss2Str.="[尾页] ";
  154. }else {
  155. $subPageCss2Str.="[下一页] ";
  156. $subPageCss2Str.="[尾页] ";
  157. }
  158. return $subPageCss2Str;
  159. }
  160. /*
  161. Construct classic mode ajax paging
  162. Current page 1/453 [Home] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page]
  163. */
  164. function subPageCss3($ fun='',$v='n'){
  165. $subPageCss2Str="";
  166. $subPageCss2Str.="Total [".$this->nums."] The current number is ".$this->current_page ."/".$this->pageNums."page";
  167. if($this->current_page > 1){
  168. //$firstPageUrl=$this->subPage_link."1";
  169. $ prewPageUrl=$this->current_page-1;
  170. $subPageCss2Str.="[ Home] ";
  171. $subPageCss2Str.="[ one page] ";
  172. }else {
  173. $subPageCss2Str.="[Home] ";
  174. $subPageCss2Str.="[Previous page] ";
  175. }
  176. $a=$this-> construct_num_Page();
  177. for($i=0;$i$s=$a[$i];
  178. if($s == $this->current_page ) {
  179. $subPageCss2Str.="[$s]";
  180. }else{
  181. $subPageCss2Str.="[$s]";
  182. }
  183. }
  184. if($this- >current_page pageNums){
  185. $lastPageUrl=$this->pageNums;
  186. $nextPageUrl=$this->current_page+1;
  187. $subPageCss2Str.=" [Next page] ";
  188. $subPageCss2Str.="[Last page] ";
  189. }else {
  190. $subPageCss2Str.="[下注one page] ";
  191. $subPageCss2Str.="[last page] ";
  192. }
  193. return $subPageCss2Str;
  194. }
  195. }
  196. ?>
Copy code

Usage: Instantiate this page class, $list = new Page (how many pieces of data per page, total amount of data, current page, array used to construct paging, link for each paging, type of paging displayed, suffix); For example $list = new Page(10,1000,1,10,’/zjjz/’,2,’.html’); echo $page = $list->subPageCss2(); It will display: Current page 1/100 [Home page] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page] Each connection is /zjjz/1.html,/zjjz/2.html... . The links of each page can be combined according to direct needs to achieve pseudo-static.

And ajax paging is

$list = new Page($pagesize,$areaAllNumber,$current_page,10,”,3); $url = $val1.’,’.$val2;//This is the value to be passed in the js function you want to trigger, $page = $list->subPageCss3(‘checkProducts’,$url);//The first parameter is the js function to be triggered. Thus achieving the effect of ajax paging.

There is also a subPageCss1 with a total of 4523 records, 10 records are displayed on each page, and the current page is 1/453 [Homepage] [Previous Page] [Next Page] [Last Page] This simple style also supports pseudo-static.



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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment