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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

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 Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software