search
HomeBackend DevelopmentPHP TutorialA practical php paging class

  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;$isub_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 sub_pages){
  79. $current_array=array();
  80. for($i=0;$ipageNums;$i++){
  81. $current_array[$i]=$i+1;
  82. }
  83. }else{
  84. $current_array=$this->initArray();
  85. if($this->current_page for($i=0;$i $current_array[$i]=$i+1;
  86. }
  87. }elseif ($this->current_page pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  88. for($i=0;$i $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  89. }
  90. }else{
  91. for($i=0;$i $current_array[$i]=$this->current_page-2+$i;
  92. }
  93. }
  94. }
  95. return $current_array;
  96. }
  97. /*
  98. 构造普通模式的分页
  99. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  100. */
  101. function subPageCss1(){
  102. $subPageCss1Str="";
  103. $subPageCss1Str.="共".$this->nums."条记录,";
  104. $subPageCss1Str.="每页显示".$this->each_disNums."条,";
  105. $subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  106. if($this->current_page > 1){
  107. $firstPageUrl=$this->subPage_link."1";
  108. $prewPageUrl=$this->subPage_link.($this->current_page-1);
  109. $subPageCss1Str.="[首页] ";
  110. $subPageCss1Str.="[上一页] ";
  111. }else {
  112. $subPageCss1Str.="[首页] ";
  113. $subPageCss1Str.="[上一页] ";
  114. }
  115. if($this->current_page pageNums){
  116. $lastPageUrl=$this->subPage_link.$this->pageNums;
  117. $nextPageUrl=$this->subPage_link.($this->current_page+1);
  118. $subPageCss1Str.=" [下一页] ";
  119. $subPageCss1Str.="[尾页] ";
  120. }else {
  121. $subPageCss1Str.="[下一页] ";
  122. $subPageCss1Str.="[尾页] ";
  123. }
  124. echo $subPageCss1Str;
  125. }
  126. /*
  127. 构造经典模式的分页
  128. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  129. */
  130. function subPageCss2(){
  131. $subPageCss2Str="";
  132. $subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  133. if($this->current_page > 1){
  134. $firstPageUrl=$this->subPage_link."1";
  135. $prewPageUrl=$this->subPage_link.($this->current_page-1);
  136. $subPageCss2Str.="[首页] ";
  137. $subPageCss2Str.="[上一页] ";
  138. }else {
  139. $subPageCss2Str.="[首页] ";
  140. $subPageCss2Str.="[上一页] ";
  141. }
  142. $a=$this->construct_num_Page();
  143. for($i=0;$i $s=$a[$i];
  144. if($s == $this->current_page ){
  145. $subPageCss2Str.="[".$s."]";
  146. }else{
  147. $url=$this->subPage_link.$s;
  148. $subPageCss2Str.="[".$s."]";
  149. }
  150. }
  151. if($this->current_page pageNums){
  152. $lastPageUrl=$this->subPage_link.$this->pageNums;
  153. $nextPageUrl=$this->subPage_link.($this->current_page+1);
  154. $subPageCss2Str.=" [下一页] ";
  155. $subPageCss2Str.="[尾页] ";
  156. }else {
  157. $subPageCss2Str.="[下一页] ";
  158. $subPageCss2Str.="[尾页] ";
  159. }
  160. echo $subPageCss2Str;
  161. }
  162. }
  163. ?>
复制代码

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


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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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),

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.