Home Backend Development PHP Tutorial Example demonstrating simple and universal PHP paging class

Example demonstrating simple and universal PHP paging class

Jul 25, 2016 am 09:12 AM

Detailed example address: http://www.alleyloft.com/contents/share?id=3 Example demonstrating simple and universal PHP paging class
  1. /***********************************************
  2. * @classname : page
  3. * @Parameters: $myde_total - total number of records
  4. * $myde_size - number of records displayed on one page
  5. * $myde_page - current page
  6. * $myde_url - get current url
  7. * @Function: paging implementation
  8. * @ Author: Song Haige
  9. */
  10. class page {
  11. private $myde_total; //Total number of records
  12. private $myde_size; //Number of records displayed on one page
  13. private $myde_page; / /Current page
  14. private $myde_page_count; //Total number of pages
  15. private $myde_i; //Number of starting pages
  16. private $myde_en; //Number of ending pages
  17. private $myde_url; //Get current url
  18. /*
  19. * $ show_pages
  20. * The format of page display, the number of pages showing links is 2*$show_pages+1.
  21. * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
  22. */
  23. private $show_pages;
  24. public function __construct($myde_total=1,$myde_size=1,$myde_page=1,$myde_url,$show_pages=2){
  25. $this->myde_total = $this->numeric($myde_total);
  26. $this->myde_size = $this->numeric($myde_size);
  27. $this->myde_page = $this->numeric($myde_page);
  28. $this->myde_page_count = ceil($this->myde_total/$this->myde_size);
  29. $this->myde_url = $myde_url;
  30. if($this->myde_total<0) $this->myde_total=0;
  31. if($this->myde_page<1) $this->myde_page=1;
  32. if($this->myde_page_count<1) $this->myde_page_count=1;
  33. if($this->myde_page>$this->myde_page_count) $this->myde_page=$this->myde_page_count;
  34. $this->limit = ($this->myde_page-1)*$this->myde_size;
  35. $this->myde_i=$this->myde_page-$show_pages;
  36. $this->myde_en=$this->myde_page+$show_pages;
  37. if($this->myde_i<1){
  38. $this->myde_en=$this->myde_en+(1-$this->myde_i);
  39. $this->myde_i=1;
  40. }
  41. if($this->myde_en>$this->myde_page_count){
  42. $this->myde_i = $this->myde_i-($this->myde_en-$this->myde_page_count);
  43. $this->myde_en=$this->myde_page_count;
  44. }
  45. if($this->myde_i<1)$this->myde_i=1;
  46. }
  47. //检测是否为数字
  48. private function numeric($num){
  49. if(strlen($num)){
  50. if(!preg_match("/^[0-9]+$/",$num)){
  51. $num=1;
  52. }else{
  53. $num = substr($num,0,11);
  54. }
  55. }else{
  56. $num=1;
  57. }
  58. return $num;
  59. }
  60. //地址替换
  61. private function page_replace($page){
  62. return str_replace("{page}",$page,$this->myde_url);
  63. }
  64. //首页
  65. private function myde_home(){
  66. if($this->myde_page!=1){
  67. return "page_replace(1)."" title="首页">首页";
  68. }else{
  69. return "

    首页

    ";
  70. }
  71. }
  72. //上一页
  73. private function myde_prev(){
  74. if($this->myde_page!=1){
  75. return "page_replace($this->myde_page-1)."" title="上一页">上一页";
  76. }else{
  77. return "

    上一页

    ";
  78. }
  79. }
  80. //下一页
  81. private function myde_next(){
  82. if($this->myde_page!=$this->myde_page_count){
  83. return "page_replace($this->myde_page+1)."" title="下一页">下一页";
  84. }else{
  85. return"

    下一页

    ";
  86. }
  87. }
  88. //尾页
  89. private function myde_last(){
  90. if($this->myde_page!=$this->myde_page_count){
  91. return "page_replace($this->myde_page_count)."" title="尾页">尾页";
  92. }else{
  93. return "

    尾页

    ";
  94. }
  95. }
  96. //输出
  97. public function myde_write($id='page'){
  98. $str ="
    ";
  99. $str.=$this->myde_home();
  100. $str.=$this->myde_prev();
  101. if($this->myde_i>1){
  102. $str.="

    ...

    ";
  103. }
  104. for($i=$this->myde_i;$i<=$this->myde_en;$i++){
  105. if($i==$this->myde_page){
  106. $str.="page_replace($i)."" title="第".$i."页" class="cur">$i";
  107. }else{
  108. $str.="page_replace($i)."" title="第".$i."页">$i";
  109. }
  110. }
  111. if( $this->myde_en<$this->myde_page_count ){
  112. $str.="

    ...

    ";
  113. }
  114. $str. =$this->myde_next();
  115. $str.=$this->myde_last();
  116. $str.="

    total".$this- >myde_page_count.

  117. "page".$this->myde_total."data

    ";
  118. $str.=" return $str;
  119. }
  120. }
  121. ?>
Copy code
  1. require_once('./page.class.php'); //Paging class
  2. $showrow = 3;//Number of rows displayed on one page
  3. $curpage = empty($_GET[' page'])?1:$_GET['page'];//The current page should also handle non-numeric situations
  4. $url = "?page={page}";//Paging address, if there are search conditions ="?page={page}&q=".$_GET['q']
  5. //The code for linking to mysql is omitted, add it yourself during testing
  6. $sql = "SELECT * FROM table";
  7. $query = mysql_query( $sql);
  8. $total = mysql_num_rows($query);//Total number of records
  9. if(!empty($_GET['page']) && $total !=0 && $curpage > ceil($total/ $showrow))
  10. $curpage = ceil($total_rows/$showrow);//The current page number is greater than the last page number, take the last page
  11. //Get the data
  12. $get_data = "select * from table limit ".($ curpage-1)*$showrow.",$showrow;";
  13. ...
  14. ?>
  15. Example demonstrates simple and universal PHP paging class
  16. < div class="main">
  • if($total>$showrow){//The total number of records is greater than the number displayed on each page, display paging
  • $page = new page($total,$showrow,$curpage,$url,2);
  • echo $page->myde_write();
  • }
  • ?>
  • 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

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

    How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

    How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

    See all articles