Home Backend Development PHP Tutorial PHP paging code, supports multiple styles and can set the paging method

PHP paging code, supports multiple styles and can set the paging method

Jul 25, 2016 am 08:52 AM

  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<0?0:$total;
  49. $this->pageSize = $pageSize<0?0:$pageSize;
  50. $this ->pageNumber = $pageNumber<0?0:$pageNumber;
  51. $this->pageParam = $pageParam;
  52. $this->calculate();
  53. }

  54. /* *

  55. * Show pagination
  56. * @access public
  57. * @return string HTML pagination string
  58. */
  59. public function pageShow(){
  60. $this->uri();
  61. if($this->pageToatl>1){
  62. if($this->page == 1){
  63. $first = ''.$this->pageShow['first'].'';
  64. $up = ''.$this->pageShow['up'].'';
  65. }else{
  66. $first = ''.$this->pageShow['first'].'';
  67. $up = ''.$this->pageShow['up'] .'';
  68. }
  69. if($this->page >= $this->pageToatl){
  70. $ending = ''.$this ->pageShow['ending'].'';
  71. $down = ''.$this->pageShow['down'].'< /span>';
  72. }else{
  73. $ending = ''.$this->pageShow['ending'].'';
  74. $down = ''.$this->pageShow['down'].'';
  75. }
  76. $input = '';
  77. $GoTo = '';
  78. }else{
  79. $first = ''; $up ='';$ending = '';$down = '';$input = '';$GoTo = '';
  80. }
  81. $this->numberF();
  82. 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);
  83. }

  84. /**

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

  139. /**

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

  163. /**

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

  175. /**

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

  191. /**

  192. * Value filter
  193. */
  194. public function __get($name){
  195. switch($name){
  196. case 'limit':
  197. case 'pageStart':
  198. case 'pageStop':
  199. return $this->$name;
  200. default:
  201. return;
  202. }
  203. }
  204. }

复制代码


Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

See all articles