Home Backend Development PHP Tutorial PHP paging class code, supports pseudo-static PHP paging class

PHP paging class code, supports pseudo-static PHP paging class

Jul 25, 2016 am 08:52 AM

  1. class Page{
  2. protected $each_disNums;//The number of entries displayed on each page
  3. protected $nums;//The total number of entries
  4. protected $current_page;//The currently selected page
  5. protected $sub_pages;//The number of pages displayed each time
  6. protected $pageNums;//Total number of pages
  7. protected $page_array = array();//Array used to construct paging
  8. protected $subPage_link;//Each paging Link
  9. protected $subPage_type;//Display the type of paging
  10. protected $houz;//Suffix
  11. /*
  12. __construct is the constructor of SubPages, which is used to run automatically when creating a class.
  13. @$each_disNums Entries displayed on each page Number
  14. @nums The total number of entries
  15. @current_num The currently selected page
  16. @sub_pagesThe number of pages displayed each time
  17. @subPage_linkThe link of each page
  18. @subPage_type displays the type of paging
  19. When @subPage_type=1, it is normal Pagination mode
  20. example: 4523 records in total, 10 records displayed on each page, current page 1/453 [Home] [Previous page] [Next page] [Last page]
  21. When @subPage_type=2, it is the classic paging style
  22. example: Current page 1/453 [Home page] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page]
  23. */
  24. function __construct($each_disNums,$nums,$current_page,$ sub_pages,$subPage_link,$subPage_type,$houz=''){
  25. $this->each_disNums=intval($each_disNums);
  26. $this->nums=intval($nums);
  27. if(!$current_page) {
  28. $this->current_page=1;
  29. }else{
  30. $this->current_page=intval($current_page);
  31. }
  32. $this->sub_pages=intval($sub_pages);
  33. $this-> ;pageNums=ceil($nums/$each_disNums);
  34. $this->subPage_link=$subPage_link;
  35. $this->show_SubPages($subPage_type);
  36. $this->houz=$houz;
  37. //echo $this->pageNums."--".$this->sub_pages;
  38. }
  39. /*
  40. __destruct destructor, called when the class is no longer in use, this function is used to release resources.
  41. */
  42. function __destruct(){
  43. unset($each_disNums);
  44. unset($nums);
  45. unset($current_page);
  46. unset($sub_pages);
  47. unset($pageNums);
  48. unset($page_array) ;
  49. unset($subPage_link);
  50. unset($subPage_type);
  51. }
  52. /*
  53. show_SubPages function is used in the constructor. And used to determine what kind of paging to display
  54. */
  55. function show_SubPages($subPage_type){
  56. if($subPage_type == 1){
  57. $this->subPageCss1();
  58. }elseif ($subPage_type == 2) {
  59. $this->subPageCss2();
  60. }
  61. }
  62. /*
  63. Function used to initialize the paging array.
  64. */
  65. function initArray(){
  66. for($i=0;$i<$this->sub_pages;$i++){
  67. $this->page_array[$i]=$i;
  68. }
  69. return $this->page_array;
  70. }
  71. /*
  72. construct_num_Page该函数使用来构造显示的条目
  73. 即使:[1][2][3][4][5][6][7][8][9][10]
  74. */
  75. function construct_num_Page(){
  76. if($this->pageNums < $this->sub_pages){
  77. $current_array=array();
  78. for($i=0;$i<$this->pageNums;$i++){
  79. $current_array[$i]=$i+1;
  80. }
  81. }else{
  82. $current_array=$this->initArray();
  83. if($this->current_page <= 3){
  84. for($i=0;$i $current_array[$i]=$i+1;
  85. }
  86. }elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  87. for($i=0;$i $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  88. }
  89. }else{
  90. for($i=0;$i $current_array[$i]=$this->current_page-2+$i;
  91. }
  92. }
  93. }
  94. return $current_array;
  95. }
  96. /*
  97. 构造普通模式的分页
  98. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  99. */ bbs.it-home.org
  100. function subPageCss1(){
  101. $subPageCss1Str="";
  102. $subPageCss1Str.="共".$this->nums."条记录,";
  103. $subPageCss1Str.="每页显示".$this->each_disNums."条,";
  104. $subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  105. if($this->current_page > 1){
  106. $firstPageUrl=$this->subPage_link."1".$this->houz;
  107. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->houz;
  108. $subPageCss1Str.="[首页] ";
  109. $subPageCss1Str.="[上一页] ";
  110. }else {
  111. $subPageCss1Str.="[首页] ";
  112. $subPageCss1Str.="[上一页] ";
  113. }
  114. if($this->current_page < $this->pageNums){
  115. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->houz;
  116. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->houz;
  117. $subPageCss1Str.=" [下一页] ";
  118. $subPageCss1Str.="[尾页] ";
  119. }else {
  120. $subPageCss1Str.="[下一页] ";
  121. $subPageCss1Str.="[尾页] ";
  122. }
  123. return $subPageCss1Str;
  124. }
  125. /*
  126. 构造经典模式的分页
  127. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  128. */
  129. function subPageCss2(){
  130. $subPageCss2Str="";
  131. $subPageCss2Str.="共[".$this->nums."]条 当前第".$this->current_page."/".$this->pageNums."页";
  132. if($this->current_page > 1){
  133. $firstPageUrl=$this->subPage_link."1".$this->houz;
  134. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->houz;
  135. $subPageCss2Str.="[首页] ";
  136. $subPageCss2Str.="[上一页] ";
  137. }else {
  138. $subPageCss2Str.="[首页] ";
  139. $subPageCss2Str.="[上一页] ";
  140. }
  141. $a=$this->construct_num_Page();
  142. for($i=0;$i$s=$a[$i];
  143. if($s == $this->current_page ){
  144. $subPageCss2Str.="[".$s."]";
  145. }else{
  146. $url=$this->subPage_link.$s.$this->houz;
  147. $subPageCss2Str.="[".$s."]";
  148. }
  149. }
  150. if($this->current_page < $this->pageNums){
  151. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->houz;
  152. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->houz;
  153. $subPageCss2Str.=" [下一页] ";
  154. $subPageCss2Str.="[尾页] ";
  155. }else {
  156. $subPageCss2Str.="[下一页] ";
  157. $subPageCss2Str.="[尾页] ";
  158. }
  159. return $subPageCss2Str;
  160. }
  161. /*
  162. Construct classic mode ajax paging
  163. Current page 1/453 [Home] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page]
  164. */
  165. function subPageCss3($ fun='',$v='n'){
  166. $subPageCss2Str="";
  167. $subPageCss2Str.="Total [".$this->nums."] The current number is ".$this->current_page ."/".$this->pageNums."page";
  168. if($this->current_page > 1){
  169. //$firstPageUrl=$this->subPage_link."1";
  170. $ prewPageUrl=$this->current_page-1;
  171. $subPageCss2Str.="[ Home] ";
  172. $subPageCss2Str.="[ one page] ";
  173. }else {
  174. $subPageCss2Str.="[Home] ";
  175. $subPageCss2Str.="[Previous page] ";
  176. }
  177. $a=$this-> construct_num_Page();
  178. for($i=0;$i$s=$a[$i];
  179. if($s == $this->current_page ) {
  180. $subPageCss2Str.="[$s]";
  181. }else{
  182. $subPageCss2Str.="[$s]";
  183. }
  184. }
  185. if($this- >current_page < $this->pageNums){
  186. $lastPageUrl=$this->pageNums;
  187. $nextPageUrl=$this->current_page+1;
  188. $subPageCss2Str.=" [Next page] ";
  189. $subPageCss2Str.="[Last page] ";
  190. }else {
  191. $subPageCss2Str.="[下注one page] ";
  192. $subPageCss2Str.="[last page] ";
  193. }
  194. return $subPageCss2Str;
  195. }
  196. }
  197. ?>
Copy code

Usage: Instantiate this page class, $list = new Page (how many pieces of data per page, total amount of data, current page, array used to construct paging, link for each paging, type of paging displayed, suffix); For example $list = new Page(10,1000,1,10,’/zjjz/’,2,’.html’); echo $page = $list->subPageCss2(); It will display: Current page 1/100 [Home page] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page] Each connection is /zjjz/1.html,/zjjz/2.html... . The links of each page can be combined according to direct needs to achieve pseudo-static.

And ajax paging is

$list = new Page($pagesize,$areaAllNumber,$current_page,10,”,3); $url = $val1.’,’.$val2;//This is the value to be passed in the js function you want to trigger, $page = $list->subPageCss3(‘checkProducts’,$url);//The first parameter is the js function to be triggered. Thus achieving the effect of ajax paging.

There is also a subPageCss1 with a total of 4523 records, 10 records are displayed on each page, and the current page is 1/453 [Homepage] [Previous Page] [Next Page] [Last Page] This simple style also supports pseudo-static.



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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

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

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

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

See all articles