search
HomeBackend DevelopmentPHP TutorialA PHP paging code (with renderings)

  1. /**

  2. * Simple paging class
  3. * Page.class.php
  4. */
  5. class Page
  6. {
  7. private $page_num; //Number of messages displayed on each page
  8. private $page_all_no; //Total number of messages
  9. private $page_len; //How many page numbers are displayed
  10. private $page; //Current page number
  11. private $page_max; //Maximum number of pages
  12. private $page_no_array; //Array of page numbers
  13. public $start_num; // The starting value of the query statement limit
  14. private $page_change; //At which page number the page number starts to increase
  15. private $URL; //Get the URL of the current page
  16. public function __construct($page_all_no, $page_num=5, $page_len=5)

  17. {
  18. $this->page_all_no = intval($page_all_no);
  19. $this->page_num = intval($page_num);
  20. $this->page_len = intval( $page_len);
  21. $this->URL = $_SERVER['REQUEST_URI'];
  22. $this->max_page(); //Get the maximum number of pages
  23. $this->page_no(); // Get the current page number
  24. $this->page_no_array(); //Page number array
  25. $this->start_num(); //Get the starting value of limit in the sql statement
  26. $this->change_page(); //Get the page number where the increment starts $this->page_change
  27. $this->getURL(); //Get the current URL and process it and return
  28. }
  29. private function isArray($str ,$str_self)

  30. {
  31. if(!is_array($str)) throw new Exception("$str_self must be an Array type");
  32. }
  33. private function page_no()

  34. {
  35. $this->page = isset($_GET['page']) ? $_GET['page'] : 1;
  36. if(isset($_GET['page']) && $_GET['page'] page = 1;
  37. if(isset($_GET['page']) && $_GET['page'] > $this->page_max) $this->page = $this->page_max;
  38. return $this->page;
  39. }
  40. private function max_page()

  41. {
  42. return $this->page_max = $this->page_all_no page_all_no/$this->page_num);
  43. }
  44. private function change_page()

  45. {
  46. return $this-> page_change = ceil($this->page_len / 2);
  47. }
  48. private function page_no_array()

  49. {
  50. return $this->page_no_array = range(1, $this-> ;page_max);
  51. }
  52. private function start_num()

  53. {
  54. return $this->start_num = $this->page_num * ($this->page - 1);
  55. }
  56. private function getURL()

  57. {
  58. if(!empty($_SERVER['argc']) ? $_SERVER['argc'] == 0 : strpos($_SERVER[ 'REQUEST_URI'], '?') === false)
  59. {
  60. $this->URL = $this->URL.'?';
  61. }else{
  62. $url_a = "/?page=[0 -9]{1,}/";
  63. $url_b = "/&page=[0-9]{1,}/";
  64. ereg("?page=[0-9]{1,}", $this ->URL) ? $this->URL = preg_replace($url_a, "?", $this->URL):
  65. ereg("&page=[0-9]{1,}", $this- >URL) ? $this->URL = preg_replace($url_b, "&", $this->URL):$this->URL = $this->URL.'&';
  66. }
  67. return $this->URL;
  68. }
  69. private function header_info($total_data_modifier='Total number:', $total_page_modifier='Total number of pages:', $current_page_modifier='Current page:' , $header_info_modifier='', $header_data_color = 'red'){

  70. if($this->page_max != 1 && $this->page_all_no != 0)
  71. {
  72. $header_info = $total_data_modifier.''.$this->page_all_no.'';
  73. $header_info .= $total_page_modifier.''.$this->page_max.'';
  74. $header_info .= $current_page_modifier.''.$this->page.' ."rn";
  75. if(!empty($ header_info_modifier)) $header_info = $header_info_modifier.$header_info;
  76. return $header_info;
  77. }else{
  78. return NULL;
  79. }
  80. }
  81. private function first($first_format = 'First page ')

  82. {
  83. return $this->page == 1 ? $first_format."rn" : ''.$first_format.''."rn";
  84. }
  85. private function last($last_format = '上一页')

  86. {
  87. if($this->page == 1) return $last_format."rn";
  88. return $this->page-1 == 1 ? ''.$last_format.''."rn" : ''.$last_format.''."rn";
  89. }
  90. private function page_num_format($separator=' ', $left_modifier='[', $right_modifier=']', $both_sides=false, $current_page_color = 'red')

  91. {
  92. empty($separator) ? $separator = ' ' : $separator;
  93. $page_array = '';
  94. if($this->page_max page_len)
  95. {
  96. for ($i=0; $i page_max; $i++)
  97. {
  98. if($this->page_no_array[$i] == $this->page)
  99. {
  100. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  101. }else{
  102. if($i == 0)
  103. {
  104. $page_one = substr($this->URL,0,strlen($this->URL)-1);
  105. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  106. }else{
  107. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  108. }
  109. }
  110. $page_array .= $the[$i].$separator;
  111. }
  112. if($both_sides === false)
  113. {
  114. $page_array = substr($page_array,0,strrpos($page_array, $separator));
  115. }elseif ($both_sides === true){
  116. $page_array = $separator.$page_array;
  117. }else{
  118. throw new Exception('ERROR: $both_sides must be a boolean type.');
  119. }
  120. }else{
  121. if($this->page page_change)
  122. {
  123. $i_start = 0;
  124. }else{
  125. $i_start = $this->page - $this->page_change;
  126. //如果最大的页码已显示,那么开始页就不会在递增
  127. if($i_start >= $this->page_max - $this->page_len){
  128. $i_start = $this->page_max - $this->page_len;
  129. }
  130. }
  131. $i_end = ($i_start+$this->page_len) - 1;
  132. for ($i = $i_start; $i {
  133. if($this->page_no_array[$i] == $this->page)
  134. {
  135. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  136. }else{
  137. if($i == 0)
  138. {
  139. $page_one = substr($this->URL,0,strlen($this->URL)-1);
  140. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  141. }else{
  142. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  143. }
  144. }
  145. $page_array .= $the[$i].$separator;
  146. }
  147. if($both_sides === false)
  148. {
  149. $page_array = substr($page_array,0,strrpos($page_array, $separator));
  150. }elseif ($both_sides === true){
  151. $page_array = $separator.$page_array;
  152. }else{
  153. throw new Exception('ERROR: $both_sides must be a boolean type.');
  154. }
  155. }
  156. return $page_array;
  157. }
  158. private function next($next_format = '下一页')

  159. {
  160. if($this->page >= $this->page_max) return $next_format."rn";
  161. return ''.$next_format.''."rn";
  162. }
  163. private function end($end_format = '最后一页')

  164. {
  165. return $this->page >= $this->page_max ? $end_format."rn" : ''.$end_format.''."rn";
  166. }
  167. public function select_page($target='self', $select_page_mode='PMA', $value_left_modifier='', $value_right_modifier='')

  168. {
  169. if($this->page_max != 1 && $this->page_all_no != 0)
  170. {
  171. if($select_page_mode === 'NORMAL')
  172. {
  173. $page_no_array = $this->page_no_array;
  174. }elseif($select_page_mode === 'PMA'){
  175. $page_no_array = $this->PMA_page_no_array();
  176. }else{
  177. throw new Exception('$select_page_mode is unknown mode');
  178. }
  179. $select_page = '';
  180. $select_page .= ''. "n";
  181. return $select_page;
  182. }else{
  183. return NULL;
  184. }
  185. }
  186. private function PMA_page_no_array()

  187. {
  188. $showAll = 200;
  189. $sliceStart = 5;
  190. $sliceEnd = 5;
  191. $percent = 20;
  192. $range = 10;
  193. if ($this->page_max $this->PMA_page_no_array = range(1, $this->page_max);

  194. } else {
  195. $this->PMA_page_no_array = array();
  196. for ($i = 1; $i $this->PMA_page_no_array[] = $i;
  197. }
  198. for ($i = $this->page_max - $sliceEnd; $i page_max; $i++) {
  199. $this->PMA_page_no_array[] = $i;
  200. }
  201. $i = $sliceStart;
  202. $x = $this->page_max - $sliceEnd;
  203. $met_boundary = false;
  204. while ($i if ($i >= ($this->page - $range) && $i page + $range)) {
  205. $i++;
  206. $met_boundary = true;
  207. } else {
  208. $i = $i + floor($this->page_max / $percent);
  209. if ($i > ($this->page - $range) && !$met_boundary) {
  210. $i = $this->page - $range;
  211. }
  212. }
  213. if ($i > 0 && $i $this->PMA_page_no_array[] = $i;
  214. }
  215. }
  216. sort($this->PMA_page_no_array);
  217. $this->PMA_page_no_array = array_unique($this->PMA_page_no_array);
  218. }
  219. return $this->PMA_page_no_array;
  220. }
  221. public function key_change_page(){

  222. echo '';
  223. }
  224. public function eshow()

  225. {
  226. return $this->last().$this->next();
  227. }
  228. public function showForHelp()

  229. {
  230. return $this->first('首页', '').$this->last('上一页', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一页', '').$this->end('尾页', '');
  231. }
  232. public function show()

  233. {
  234. return $this->header_info().$this->first('首页', '').$this->last('上一页', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一页', '').$this->end('尾页', '');
  235. }
  236. }
复制代码

2. How to use php paging class:

  1. header('content-type:text/html;charset=utf-8');
  2. include('Page.class.php');
  3. $page_all_no = 12000; //Total number of data items
  4. $page_num = 25; //Set the number of items displayed on each page
  5. $page_len = 7; //The maximum number of page numbers to display
  6. $page = new Page($page_all_no, $page_num, $page_len);
  7. $start_num = $ page->start_num;
  8. $sql = "select * from table limit {$start_num}, {$page_num}";
  9. $page->key_change_page(); //arrow keys to turn pages
  10. var_dump($sql);
  11. //It comes with three paging forms, you can add new methods as needed, call
  12. echo '

    '.$page->select_page('self', 'NORMAL').''; //The second parameter here defaults to PMA mode. The difference between the two modes is given above

  13. echo '

    '.$page->eshow().'

    > ;';
  14. echo '

    '.$page->show().'

    ';
Copy code

3, PHP paging class, rendering: A PHP paging code (with renderings)



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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

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

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.