search
HomeBackend DevelopmentPHP TutorialPHP paging code, supports multiple styles and can set the paging method

  1. class Page {

  2. private $total;//Total quantity
  3. private $limit;//Return the limit statement of mysql
  4. private $pageStart;//Starting value
  5. private $pageStop;//Ending value
  6. private $pageNumber;//Display the number of paging numbers
  7. private $page;//Current page
  8. private $pageSize;//The number displayed on each page
  9. private $pageToatl;//Paging The total number of
  10. private $pageParam;//Paging variables
  11. private $uri;//URL parameters
  12. /**
  13. * The paging setting style is not case-sensitive
  14. * %pageToatl% //Total number of pages
  15. * %page%//Current page
  16. * %pageSize% //Number of data items displayed on the current page
  17. * %pageStart%//This page Starting number
  18. * %pageStop%//Ending number of this page
  19. * %total%//Total number of data
  20. * %first%//Homepage
  21. * %ending%//Last page
  22. * %up%/ /Previous page
  23. * %down%//Next page
  24. * %F%//Start page
  25. * %E%//End page
  26. * %omitFA%//Omit and jump before
  27. * %omitEA% //Omit after and jump
  28. * %omitF%//Omit before
  29. * %omitE%//Omit after
  30. * %numberF%//Fixed number of number pagination
  31. * %numberD%//Equal left and right number pagination
  32. * %input%//Jump input box
  33. * %GoTo%//Jump button
  34. */ bbs.it-home.org
  35. private $pageType = 'Page %page%/Total%pageToatl%%first%%up%%F%%omitFA%%numberF%%omitEA%%E%%down%%ending%';
  36. / /Display value setting
  37. private $pageShow = array('first'=>'Home page','ending'=>'Last page','up'=>'Previous page','down'=> 'Next page','GoTo'=>'GO');
  38. /**

  39. * Initialization data, construction method
  40. * @access public
  41. * @param int $total The total number of data
  42. * @param int $pageSize The number of items displayed on each page
  43. * @param int $pageNumber The number of paging numbers displayed (use %numberF% It will have different effects than using %numberD%)
  44. * @param string $pageParam paging variable
  45. * @return void
  46. */
  47. public function __construct($total,$pageSize=10,$pageNumber =5,$pageParam='p'){
  48. $this->total = $total$this->pageSize = $pageSize$this ->pageNumber = $pageNumber$this->pageParam = $pageParam;
  49. $this->calculate();
  50. }
  51. /* *

  52. * Show pagination
  53. * @access public
  54. * @return string HTML pagination string
  55. */
  56. public function pageShow(){
  57. $this->uri();
  58. if($this->pageToatl>1){
  59. if($this->page == 1){
  60. $first = ''.$this->pageShow['first'].'';
  61. $up = ''.$this->pageShow['up'].'';
  62. }else{
  63. $first = ''.$this->pageShow['first'].'';
  64. $up = ''.$this->pageShow['up'] .'';
  65. }
  66. if($this->page >= $this->pageToatl){
  67. $ending = ''.$this ->pageShow['ending'].'';
  68. $down = ''.$this->pageShow['down'].'';
  69. }else{
  70. $ending = ''.$this->pageShow['ending'].'';
  71. $down = ''.$this->pageShow['down'].'';
  72. }
  73. $input = '';
  74. $GoTo = '';
  75. }else{
  76. $first = ''; $up ='';$ending = '';$down = '';$input = '';$GoTo = '';
  77. }
  78. $this->numberF();
  79. return str_ireplace(array('%pageToatl%','%page%','%pageSize%','%pageStart%','%pageStop%','%total%','%first%','%ending%','%up%','%down%','%input%','%GoTo%'),array($this->pageToatl,$this->page,$this->pageStop-$this->pageStart,$this->pageStart,$this->pageStop,$this->total,$first,$ending,$up,$down,$input,$GoTo),$this->pageType);
  80. }
  81. /**

  82. *Number Pagination
  83. */
  84. private function numberF(){
  85. $pageF = stripos($this->pageType,'%numberF%');
  86. $pageD = stripos($this->pageType,'%numberD%');
  87. $numberF = '';$numberD = '';$F = '';$E ='';$omitF = '';$omitFA = '';$omitE = '';$omitEA = '';
  88. if($pageF!==false || $pageD!==false){
  89. if($pageF!==false){
  90. $number = $this->pageNumber%2==0?$this->pageNumber/2:($this->pageNumber+1)/2;
  91. $DStart = $this->page - $numberpage - $number-1:0;
  92. if($this->page+$number-$DStart > $this->pageToatl){
  93. $DStop = ($this->page+$number-$DStart) - $this->pageToatl;
  94. $forStop = $this->pageToatl+1;
  95. }else{
  96. $DStop = 0;
  97. $forStop = $this->page+$number-$DStart+1;
  98. }
  99. $forStart = $this->page-$number-$DStoppage-$number-$DStop;
  100. for($i=$forStart;$iif($i==$this->page){
  101. $numberF .= ''.$i.'';
  102. }else{
  103. $numberF .= ''.$i.'';
  104. }
  105. }
  106. }
  107. if($pageD!==false){
  108. $number = $this->pageNumber;
  109. $forStart = $this->page-$number>0?$this->page-$number:1;
  110. $forStop = $this->page+$number>$this->pageToatl?$this->pageToatl+1:$this->page+$number+1;
  111. for($i=$forStart;$ipage;++$i){
  112. $numberD .= ''.$i.'';
  113. }
  114. $numberD .= ''.$this->page.'';
  115. $start = $this->page+1;
  116. for($i=$start;$i$numberD .= ''.$i.'';
  117. }
  118. }
  119. $F = $forStart>1?'1':'';
  120. $E = $forStoppageToatl+1?''.$this->pageToatl.'':'';
  121. if($forStart>2){
  122. $omitF = '';
  123. $startA = $this->page-$numberpage-$number;
  124. $omitFA = '';
  125. }
  126. if($forStoppageToatl){
  127. $omitE = '';
  128. $stopA = $this->page+$number>$this->pageToatl?$this->pageToatl:$this->page+$number;
  129. $omitEA = '';
  130. }
  131. }
  132. $this->pageType = str_ireplace(array('%F%','%E%','%omitFA%','%omitEA%','%omitF%','%omitE%','%numberF%','%numberD%'),array($F,$E,$omitFA,$omitEA,$omitF,$omitE,$numberF,$numberD),$this->pageType);
  133. }
  134. /**

  135. *Methods for processing url
  136. * @access public
  137. * @param array $url Keep the URL directly in the relationship array
  138. * @return string filtered url tail parameters
  139. */
  140. private function uri(){
  141. $url = $_SERVER["REQUEST_URI"];
  142. $par = parse_url($url);
  143. if (isset($par['query'])) {
  144. parse_str($par['query'],$query);
  145. if(!is_array($this->uri)){
  146. $this->uri = array();
  147. }
  148. array_merge($query,$this->uri);
  149. unset($query[$this->pageParam]);
  150. while($key = array_search('',$query)){
  151. unset($query[$key]);
  152. }
  153. $this->uri = $par['path'].'?'.http_build_query($query);
  154. }else{
  155. $this->uri = $par['path'].'?';
  156. }
  157. }
  158. /**

  159. * Set the limit method and calculate the starting number and ending number
  160. */
  161. private function calculate(){
  162. $this->pageToatl = ceil($this->total/$this->pageSize);
  163. $this->page = intval($_GET[$this->pageParam]);
  164. $this->page = $this->page>=1?$this->page>$this->pageToatl?$this->pageToatl:$this->page:1;
  165. $this->pageStart = ($this->page-1)*$this->pageSize;
  166. $this->pageStop = $this->pageStart+$this->pageSize;
  167. $this->pageStop = $this->pageStop>$this->total?$this->total:$this->pageStop;
  168. $this->limit = $this->pageStart.','.$this->pageStop;
  169. }
  170. /**

  171. * Set filters
  172. */
  173. public function __set($name,$value){
  174. switch($name){
  175. case 'pageType':
  176. case 'uri':
  177. $this->$name = $value;
  178. return;
  179. case 'pageShow':
  180. if(is_array($value)){
  181. $this->pageShow = array_merge($this->pageShow,$value);
  182. }
  183. return;
  184. }
  185. }
  186. /**

  187. * Value filter
  188. */
  189. public function __get($name){
  190. switch($name){
  191. case 'limit':
  192. case 'pageStart':
  193. case 'pageStop':
  194. return $this->$name;
  195. default:
  196. return;
  197. }
  198. }
  199. }
复制代码


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

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

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

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

Hot Tools

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.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)