Home Backend Development PHP Tutorial Pagination class - tail page number navigation

Pagination class - tail page number navigation

Jul 25, 2016 am 09:01 AM

Pagination class - tail page number navigation
  1. /**
  2. * Generate the tail paging navigation of Comment
  3. * @author Li Jun
  4. *
  5. */
  6. class cmtTail{
  7. private $currentPage;
  8. private $totalPage;
  9. /**
  10. * Generate page number navigation - total control function
  11. * @param string $currentPage current page number
  12. * @param string $totalPage total page number
  13. * @throws Exception If the page number is less than 1, an exception will be thrown
  14. * @return string
  15. */
  16. function __do($currentPage, $totalPage ) {
  17. $this->currentPage=$currentPage;
  18. $this->totalPage=$totalPage;
  19. if($this->totalPage<=10){//The total number of pages is less than or equal to 10 pages
  20. if( $this->currentPage==1){//The current page is the first page
  21. $str='previous page'.$this->currentTag();
  22. for ($i = 2; $i < = $this->totalPage; $i++) {
  23. $str=$str.$this->commonTag($i);
  24. }
  25. $str=$str.$this->next();
  26. } elseif ($this->currentPage==$this->totalPage){//Jumped to the last page
  27. $str=$this->up();
  28. for ($i = 1; $i < ;= $this->totalPage-1; $i++) {
  29. $str=$str.$this->commonTag($i);
  30. }
  31. $str=$str.$this->currentTag() ;
  32. $str=$str.$this->next();
  33. }else{
  34. $str=$this->up();
  35. for($i=1; $i<$this-> currentPage; $i++){
  36. $str=$str.$this->commonTag($i);
  37. }
  38. $str=$str.$this->currentTag();//Generate the current page tag
  39. for ($i = $this->currentPage+1; $i <= $this->totalPage; $i++) {
  40. $str=$str.$this->commonTag($i);
  41. }
  42. $str=$str.$this->next();
  43. }
  44. }elseif ($this->totalPage>10){//The page number is greater than 10
  45. if($this->currentPage==1){ //8+2
  46. $str='previous page'.$this->currentTag();
  47. for ($i = 2; $i <= 8; $i++) {
  48. $str=$str. $this->commonTag($i);
  49. }
  50. $str=$str.'...';//Add ellipses
  51. $str=$str.$this->commonTag($this->totalPage -1);
  52. $str=$str.$this->commonTag($this->totalPage);
  53. }elseif($this->currentPage==$this->totalPage) {//currently Last page
  54. $str=$this->up();
  55. $str=$str.$this->commonTag(1);
  56. $str=$str.'...';//Add ellipses
  57. for ($i = $this->totalPage-6; $i < $this->totalPage; $i++) {
  58. $str=$str.$this->commonTag($i);
  59. }
  60. $str=$str.$this->currentTag();
  61. $str=$str.$this->next();
  62. }else {
  63. if ($this->currentPage<6) {
  64. $str=$this->up();
  65. for ($i = 1; $i < $this->currentPage; $i++) {
  66. $str=$str.$this->commonTag($ i);
  67. }
  68. $str=$str.$this->currentTag();
  69. for ($i = $this->currentPage+1; $i <= 7; $i++) {
  70. $str =$str.$this->commonTag($i);
  71. }
  72. $str=$str.'...';//Add ellipses
  73. $str=$str.$this->commonTag($this ->totalPage);
  74. $str=$str.$this->next();
  75. }else {
  76. if ($this->currentPage>=$this->totalPage-4) {
  77. $str =$this->up();
  78. $str=$str.$this->commonTag(1);
  79. $str=$str.'...';//Add ellipses
  80. for ($i = $this->totalPage-6; $i < $this->currentPage; $i++) {
  81. $str=$str.$this->commonTag($i);
  82. }
  83. $str=$str .$this->currentTag();
  84. for ($i = $this->currentPage+1; $i <= $this->totalPage; $i++) {
  85. $str=$str.$this ->commonTag($i);
  86. }
  87. $str=$str.$this->next();
  88. }elseif ($this->currentPage<$this->totalPage-4){// 1+5+1
  89. $str=$this->up();
  90. $str=$str.$this->commonTag(1);
  91. $str=$str.'...';// Add ellipsis
  92. $str=$str.$this->commonTag($this->currentPage-2);
  93. $str=$str.$this->commonTag($this->currentPage-1);
  94. $str=$str.$this->currentTag();
  95. $str=$str.$this->commonTag($this->currentPage+1);
  96. $str=$str.$this- >commonTag($this->currentPage+2);
  97. $str=$str.'...';//Add ellipses
  98. $str=$str.$this->commonTag($this-> totalPage);
  99. $str=$str.$this->next();
  100. }
  101. };
  102. }
  103. }elseif ($this->totalPage<=0){
  104. throw new Exception("Page impossible Less than 1");
  105. }
  106. return $str;
  107. }
  108. /**
  109. * General tag
  110. * @param int $param page number
  111. * @return string
  112. */
  113. function commonTag($param) {
  114. return ''.$param.'';
  115. }
  116. /**
  117. * Generate current page label
  118. * @param int $param page number
  119. * @return string
  120. */
  121. function currentTag() {
  122. return ''.$this->currentPage.'';
  123. }
  124. /**
  125. * Generate next page label
  126. * @param int $param Next page page number
  127. * @return string
  128. */
  129. function next() {
  130. if ($this->currentPage==$this->totalPage) {
  131. return '下一页';
  132. }
  133. return '下一页';
  134. }
  135. /**
  136. * Generate previous page label
  137. * @param int $param Previous page page number
  138. * @return string
  139. */
  140. function up() {
  141. if ($this->currentPage==1){
  142. return '上一页';
  143. }else{
  144. return '上一页';
  145. }
  146. }
  147. /**
  148. * Instantiate cmtTail,
  149. * Function: Generate tail paging navigation of Comment
  150. * @param string $currentPage current page number
  151. * @param string $totalPage total number of pages
  152. * @return string return html tag string
  153. */
  154. static function GO($currentPage, $totalPage) {
  155. $p=new cmtTail();
  156. return $p->__do($currentPage, $totalPage);
  157. }
  158. }
复制代码


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�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles