search
HomeBackend DevelopmentPHP TutorialExample demonstrating simple and universal PHP paging class

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_totalmyde_total=0;
  31. if($this->myde_pagemyde_page=1;
  32. if($this->myde_page_countmyde_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 $this->myde_en=$this->myde_en+(1-$this->myde_i);
  38. $this->myde_i=1;
  39. }
  40. if($this->myde_en>$this->myde_page_count){
  41. $this->myde_i = $this->myde_i-($this->myde_en-$this->myde_page_count);
  42. $this->myde_en=$this->myde_page_count;
  43. }
  44. if($this->myde_imyde_i=1;
  45. }
  46. //检测是否为数字
  47. private function numeric($num){
  48. if(strlen($num)){
  49. if(!preg_match("/^[0-9]+$/",$num)){
  50. $num=1;
  51. }else{
  52. $num = substr($num,0,11);
  53. }
  54. }else{
  55. $num=1;
  56. }
  57. return $num;
  58. }
  59. //地址替换
  60. private function page_replace($page){
  61. return str_replace("{page}",$page,$this->myde_url);
  62. }
  63. //首页
  64. private function myde_home(){
  65. if($this->myde_page!=1){
  66. return "page_replace(1)."" title="首页">首页";
  67. }else{
  68. return "

    首页

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

    上一页

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

    下一页

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

    尾页

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

    ...

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

    ...

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

    total".$this- >myde_page_count.

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

    ";
  118. $str.="
> ;";
  • return $str;
  • }
  • }
  • ?>
  • 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
  • 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
    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
    The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

    What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

    PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

    PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    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

    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.

    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

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.