search
HomeBackend DevelopmentPHP TutorialPHP paging class and paging function, multiple paging styles to choose from

  1. /**

  2. * Page name: page_class.php
  3. */
  4. class Page {
  5. private $each_disNums; //Number of entries displayed per page
  6. private $nums; //Total number of entries
  7. private $current_page; //The currently selected page
  8. private $sub_pages; //Number of pages displayed each time
  9. private $pageNums; //Total number of pages
  10. private $page_array = array (); //Array used to construct paging
  11. private $ subPage_link; //Links for each page
  12. /**
  13. *
  14. * __construct is the constructor of SubPages, used to run automatically when creating a class.
  15. * @$each_disNums The number of entries displayed on each page
  16. * @nums The total number of entries
  17. * @current_num The currently selected page
  18. * @sub_pages The number of pages displayed each time
  19. * @subPage_link The link of each paging
  20. * @subPage_type Displays the type of paging
  21. *
  22. * When @subPage_type=1, it is normal paging mode
  23. * example: 4523 records in total, each The page displays 10 items, 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: The 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 Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) {
  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. }
  39. / **
  40. * Take care of low version
  41. */
  42. function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) {
  43. $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) ;
  44. }
  45. /*

  46. __destruct destructor, which is called when the class is no longer in use. This function is used to release resources.
  47. */
  48. function __destruct() {
  49. unset ($each_disNums);
  50. unset ($nums);
  51. unset ($current_page);
  52. unset ($sub_pages);
  53. unset ($pageNums);
  54. unset ($page_array) ;
  55. unset ($subPage_link);
  56. }
  57. /*

  58. Function used to initialize the paging array.
  59. */
  60. function initArray() {
  61. for ($i = 0; $i sub_pages; $i++) {
  62. $this->page_array[$i] = $i;
  63. }
  64. return $this->page_array;
  65. }
  66. /*

  67. construct_num_Page该函数使用来构造显示的条目
  68. 即使:[1][2][3][4][5][6][7][8][9][10]
  69. */
  70. function construct_num_Page() {
  71. if ($this->pageNums sub_pages) {
  72. $current_array = array ();
  73. for ($i = 0; $i pageNums; $i++) {
  74. $current_array[$i] = $i +1;
  75. }
  76. } else {
  77. $current_array = $this->initArray();
  78. if ($this->current_page for ($i = 0; $i $current_array[$i] = $i +1;
  79. }
  80. }
  81. elseif ($this->current_page pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
  82. for ($i = 0; $i $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
  83. }
  84. } else {
  85. for ($i = 0; $i $current_array[$i] = $this->current_page - 2 + $i;
  86. }
  87. }
  88. }
  89. return $current_array;

  90. }
  91. /*

  92. 构造普通模式的分页
  93. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  94. */
  95. function subPageCss1() {
  96. $subPageCss1Str = "";
  97. $subPageCss1Str .= "共" . $this->nums . "条记录,";
  98. $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";
  99. $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  100. if ($this->current_page > 1) {
  101. $firstPageUrl = $this->subPage_link . "1";
  102. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  103. $subPageCss1Str .= "[首页] ";
  104. $subPageCss1Str .= "[上一页] ";
  105. } else {
  106. $subPageCss1Str .= "[首页] ";
  107. $subPageCss1Str .= "[上一页] ";
  108. }
  109. if ($this->current_page pageNums) {

  110. $lastPageUrl = $this->subPage_link . $this->pageNums;
  111. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  112. $subPageCss1Str .= " [下一页] ";
  113. $subPageCss1Str .= "[尾页] ";
  114. } else {
  115. $subPageCss1Str .= "[下一页] ";
  116. $subPageCss1Str .= "[尾页] ";
  117. }
  118. return $subPageCss1Str;

  119. }

  120. /*

  121. 构造经典模式的分页
  122. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  123. */
  124. function subPageCss2() {
  125. $subPageCss2Str = "";
  126. $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  127. if ($this->current_page > 1) {

  128. $firstPageUrl = $this->subPage_link . "1";
  129. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  130. $subPageCss2Str .= "[首页] ";
  131. $subPageCss2Str .= "[上一页] ";
  132. } else {
  133. $subPageCss2Str .= "[首页] ";
  134. $subPageCss2Str .= "[上一页] ";
  135. }
  136. $a = $this->construct_num_Page();

  137. for ($i = 0; $i $s = $a[$i];
  138. if ($s == $this->current_page) {
  139. $subPageCss2Str .= "[" . $s . "]";
  140. } else {
  141. $url = $this->subPage_link . $s;
  142. $subPageCss2Str .= "[" . $s . "]";
  143. }
  144. }
  145. if ($this->current_page pageNums) {

  146. $lastPageUrl = $this->subPage_link . $this->pageNums;
  147. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  148. $subPageCss2Str .= " [下一页] ";
  149. $subPageCss2Str .= "[尾页] ";
  150. } else {
  151. $subPageCss2Str .= "[下一页] ";
  152. $subPageCss2Str .= "[尾页] ";
  153. }
  154. return $subPageCss2Str;
  155. }
  156. }
  157. //Test two different effects

  158. $t = new Page(10, 100, $_GET['p'], 5, 'page_class. php?p=');
  159. echo $t->subPageCss2(); //Call the classic paging function
  160. echo "
    ";

  161. echo $t->subPageCss1 ();//Call the normal paging function
Copy code


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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor