search
HomeBackend DevelopmentPHP TutorialPractical php paging class, multiple paging styles

复制代码
  1. class SubPages{
  2. private $each_disNums; //The number of entries displayed on each page
  3. private $nums; //Total number of entries
  4. private $current_page; //The currently selected page
  5. private $sub_pages;//Number of pages displayed each time
  6. private $pageNums;//Total number of pages
  7. private $point;//Total number of pages
  8. private $page_array = array();//Array used to construct paging
  9. private $subPage_link;//The link of each page
  10. private $subPage_type;//Display the type of page
  11. / *
  12. __construct is the constructor of SubPages, which is used to run automatically when creating the class.
  13. @$each_disNums Displayed on each page Number of entries
  14. @nums Total number of entries
  15. @current_num Currently selected page
  16. @sub_pages Number of pages displayed each time
  17. @subPage_link Link to each page
  18. @subPage_type Display type of paging
  19. When @subPage_type=1 Normal paging 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,$point){
  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->point=$point;
  36. $this->show_SubPages($subPage_type);
  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 array for creating paging.
  64. * /
  65. function initArray(){
  66. for($i=0;$isub_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 sub_pages){
  77. $current_array=array();
  78. for($i=0;$ipageNums;$i++){
  79. $current_array[$i]=$i+1;
  80. }
  81. }else{
  82. $current_array=$this->initArray();
  83. if($this->current_page for($i=0;$i $current_array[$i]=$i+1;
  84. }
  85. }elseif ($this->current_page pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  86. for($i=0;$i $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  87. }
  88. }else{
  89. for($i=0;$i $current_array[$i]=$this->current_page-2+$i;
  90. }
  91. }
  92. }
  93. return $current_array;
  94. }
  95. / *
  96. 构造经典模式的分页
  97. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  98. * /
  99. function subPageCss2(){
  100. $subPageCss2Str="";
  101. //2462347条商家信息
  102. $subPageCss2Str.= "当前第".$this->current_page." /".$this->pageNums." 页     共".$this->nums."条商家信息";
  103. //$subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  104. if($this->current_page > 1){
  105. $firstPageUrl=$this->subPage_link."1"."#zkfb_shop";
  106. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->point;
  107. //首页上一页
  108. $subPageCss2Str.="首页";
  109. $subPageCss2Str.="上一页";
  110. }else {
  111. $subPageCss2Str.="首页";
  112. $subPageCss2Str.="上一页";
  113. }
  114. $a=$this->construct_num_Page();
  115. for($i=0;$i $s=$a[$i];
  116. if($s == $this->current_page ){
  117. // $subPageCss2Str.="[".$s."]";
  118. $subPageCss2Str.="".$s."";
  119. }else{
  120. $url=$this->subPage_link.$s.$this->point;
  121. // 2
  122. $subPageCss2Str.="".$s."";
  123. // $subPageCss2Str.="[".$s."]";
  124. }
  125. }
  126. if($this->current_page pageNums){
  127. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->point;
  128. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->point;
  129. //下一页末页
  130. $subPageCss2Str.="下一页";
  131. $subPageCss2Str.="尾页
  132. ";
  133. }else {
  134. $subPageCss2Str.="下一页";
  135. $subPageCss2Str.="尾页
  136. ";
  137. }
  138. echo $subPageCss2Str;
  139. }
  140. }
  141. ?>
复制代码


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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools