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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

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-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

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' =>

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.

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

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

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尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool