Home Backend Development PHP Tutorial PHP paging class and paging function, multiple paging styles to choose from

PHP paging class and paging function, multiple paging styles to choose from

Jul 25, 2016 am 08:51 AM

  1. /**

  2. * Page name: page_class.php
  3. */
  4. class Page {
  5. private $each_disNums; //Number of entries displayed per page
  6. private $nums; //Total number of entries
  7. private $current_page; //The currently selected page
  8. private $sub_pages; //Number of pages displayed each time
  9. private $pageNums; //Total number of pages
  10. private $page_array = array (); //Array used to construct paging
  11. private $ subPage_link; //Links for each page
  12. /**
  13. *
  14. * __construct is the constructor of SubPages, used to run automatically when creating a class.
  15. * @$each_disNums The number of entries displayed on each page
  16. * @nums The total number of entries
  17. * @current_num The currently selected page
  18. * @sub_pages The number of pages displayed each time
  19. * @subPage_link The link of each paging
  20. * @subPage_type Displays the type of paging
  21. *
  22. * When @subPage_type=1, it is normal paging mode
  23. * example: 4523 records in total, each The page displays 10 items, the current page 1/453 [Home] [Previous page] [Next page] [Last page]
  24. * When @subPage_type=2, it is the classic paging style
  25. * example: The current page 1/453 [ Home page] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page]
  26. */
  27. function Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) {
  28. $this->each_disNums = intval($each_disNums );
  29. $this->nums = intval($nums);
  30. if (!$current_page) {
  31. $this->current_page = 1;
  32. } else {
  33. $this->current_page = intval($current_page );
  34. }
  35. $this->sub_pages = intval($sub_pages);
  36. $this->pageNums = ceil($nums / $each_disNums);
  37. $this->subPage_link = $subPage_link;
  38. }
  39. / **
  40. * Take care of low version
  41. */
  42. function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) {
  43. $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) ;
  44. }

  45. /*

  46. __destruct destructor, which is called when the class is no longer in use. This function is used to release resources.
  47. */
  48. function __destruct() {
  49. unset ($each_disNums);
  50. unset ($nums);
  51. unset ($current_page);
  52. unset ($sub_pages);
  53. unset ($pageNums);
  54. unset ($page_array) ;
  55. unset ($subPage_link);
  56. }

  57. /*

  58. Function used to initialize the paging array.
  59. */
  60. function initArray() {
  61. for ($i = 0; $i < $this->sub_pages; $i++) {
  62. $this->page_array[$i] = $i;
  63. }
  64. return $this->page_array;
  65. }

  66. /*

  67. construct_num_Page该函数使用来构造显示的条目
  68. 即使:[1][2][3][4][5][6][7][8][9][10]
  69. */
  70. function construct_num_Page() {
  71. if ($this->pageNums < $this->sub_pages) {
  72. $current_array = array ();
  73. for ($i = 0; $i < $this->pageNums; $i++) {
  74. $current_array[$i] = $i +1;
  75. }
  76. } else {
  77. $current_array = $this->initArray();
  78. if ($this->current_page <= 3) {
  79. for ($i = 0; $i < count($current_array); $i++) {
  80. $current_array[$i] = $i +1;
  81. }
  82. }
  83. elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
  84. for ($i = 0; $i < count($current_array); $i++) {
  85. $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
  86. }
  87. } else {
  88. for ($i = 0; $i < count($current_array); $i++) {
  89. $current_array[$i] = $this->current_page - 2 + $i;
  90. }
  91. }
  92. }

  93. return $current_array;

  94. }

  95. /*

  96. 构造普通模式的分页
  97. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  98. */
  99. function subPageCss1() {
  100. $subPageCss1Str = "";
  101. $subPageCss1Str .= "共" . $this->nums . "条记录,";
  102. $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";
  103. $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  104. if ($this->current_page > 1) {
  105. $firstPageUrl = $this->subPage_link . "1";
  106. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  107. $subPageCss1Str .= "[首页] ";
  108. $subPageCss1Str .= "[上一页] ";
  109. } else {
  110. $subPageCss1Str .= "[首页] ";
  111. $subPageCss1Str .= "[上一页] ";
  112. }

  113. if ($this->current_page < $this->pageNums) {

  114. $lastPageUrl = $this->subPage_link . $this->pageNums;
  115. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  116. $subPageCss1Str .= " [下一页] ";
  117. $subPageCss1Str .= "[尾页] ";
  118. } else {
  119. $subPageCss1Str .= "[下一页] ";
  120. $subPageCss1Str .= "[尾页] ";
  121. }

  122. return $subPageCss1Str;

  123. }

  124. /*

  125. 构造经典模式的分页
  126. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  127. */
  128. function subPageCss2() {
  129. $subPageCss2Str = "";
  130. $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";

  131. if ($this->current_page > 1) {

  132. $firstPageUrl = $this->subPage_link . "1";
  133. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  134. $subPageCss2Str .= "[首页] ";
  135. $subPageCss2Str .= "[上一页] ";
  136. } else {
  137. $subPageCss2Str .= "[首页] ";
  138. $subPageCss2Str .= "[上一页] ";
  139. }

  140. $a = $this->construct_num_Page();

  141. for ($i = 0; $i < count($a); $i++) {
  142. $s = $a[$i];
  143. if ($s == $this->current_page) {
  144. $subPageCss2Str .= "[" . $s . "]";
  145. } else {
  146. $url = $this->subPage_link . $s;
  147. $subPageCss2Str .= "[" . $s . "]";
  148. }
  149. }

  150. if ($this->current_page < $this->pageNums) {

  151. $lastPageUrl = $this->subPage_link . $this->pageNums;
  152. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  153. $subPageCss2Str .= " [下一页] ";
  154. $subPageCss2Str .= "[尾页] ";
  155. } else {
  156. $subPageCss2Str .= "[下一页] ";
  157. $subPageCss2Str .= "[尾页] ";
  158. }
  159. return $subPageCss2Str;
  160. }
  161. }

  162. //Test two different effects

  163. $t = new Page(10, 100, $_GET['p'], 5, 'page_class. php?p=');
  164. echo $t->subPageCss2(); //Call the classic paging function

  165. echo "
    ";

  166. echo $t->subPageCss1 ();//Call the normal paging function

Copy code


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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles