Home Backend Development PHP Tutorial PHP paging code, PHP paging code with beautiful styles

PHP paging code, PHP paging code with beautiful styles

Jul 25, 2016 am 08:52 AM

  1. include "pager.class.php";
  2. $CurrentPage=isset($_GET['page'])?$_GET['page']:1;
  3. //die($CurrentPage);
  4. $myPage=new pager(1300,intval($CurrentPage));
  5. $pageStr= $myPage->GetPagerContent();
  6. //echo $pageStr;
  7. $myPage=new pager(90,intval($CurrentPage));
  8. $pageStr= $myPage->GetPagerContent();
  9. echo $pageStr;
  10. ?>
复制代码
  1. php分页演示-bbs.it-home.org
  2. include "pager.class.php";
  3. $CurrentPage=isset($_GET['page'])?$_GET['page']:1;
  4. //die($CurrentPage);
  5. $myPage=new pager(1300,intval($CurrentPage));
  6. $pageStr= $myPage->GetPagerContent();
  7. //echo $pageStr;
  8. $myPage=new pager(90,intval($CurrentPage));
  9. $pageStr= $myPage->GetPagerContent();
  10. echo $pageStr;
  11. ?>
复制代码

2) pager.class.php:

  1. /*

  2. * PHP分页类
  3. * @package Page
  4. * @Created 2013-03-27
  5. * @Modify 2013-03-27
  6. * Example:
  7. $myPage=new Pager(1300,intval($CurrentPage));
  8. $pageStr= $myPage->GetPagerContent();
  9. echo $pageStr;
  10. */
  11. class Pager {
  12. private $pageSize = 10;
  13. private $pageIndex;
  14. private $totalNum;

  15. private $totalPagesCount;

  16. private $pageUrl;

  17. private static $_instance;

  18. public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 10,$p_initNum=3,$p_initMaxNum=5) {

  19. if (! isset ( $p_totalNum ) || !isset($p_pageIndex)) {
  20. die ( "pager initial error" );
  21. }

  22. $this->totalNum = $p_totalNum;

  23. $this->pageIndex = $p_pageIndex;
  24. $this->pageSize = $p_pageSize;
  25. $this->initNum=$p_initNum;
  26. $this->initMaxNum=$p_initMaxNum;
  27. $this->totalPagesCount= ceil($p_totalNum / $p_pageSize);
  28. $this->pageUrl=$this->_getPageUrl();

  29. $this->_initPagerLegal();

  30. }

  31. /**
  32. * Get the current URL string with the page part removed
  33. *
  34. * @return String URL string
  35. */
  36. private function _getPageUrl() {
  37. $CurrentUrl = $_SERVER["REQUEST_URI"];
  38. $arrUrl = parse_url($CurrentUrl);
  39. $urlQuery = $arrUrl["query"];

  40. if($urlQuery){

  41. $urlQuery = ereg_replace("(^|&)page=" . $this->pageIndex, "", $urlQuery);
  42. $CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl);

  43. if($urlQuery){

  44. $CurrentUrl.="&page";
  45. }
  46. else $CurrentUrl.="page";

  47. } else {

  48. $CurrentUrl.="?page";
  49. }

  50. return $CurrentUrl;

  51. }

  52. /*
  53. *设置页面参数合法性
  54. *@return void
  55. */
  56. private function _initPagerLegal()
  57. {
  58. if((!is_numeric($this->pageIndex)) || $this->pageIndex<1)
  59. {
  60. $this->pageIndex=1;
  61. }elseif($this->pageIndex > $this->totalPagesCount)
  62. {
  63. $this->pageIndex=$this->totalPagesCount;
  64. }

  65. }

  66. //$this->pageUrl}={$i}
  67. //{$this->CurrentUrl}={$this->TotalPages}
  68. public function GetPagerContent() {
  69. $str = "
  70. //首页 上一页
  71. if($this->pageIndex==1)
  72. {
  73. $str .="首页 "."n";
  74. $str .="上一页 "."n"."n";
  75. }else
  76. {
  77. $str .="首页 "."n";
  78. $str .="上一页 "."n"."n";
  79. }
  80. /*

  81. 除首末后 页面分页逻辑

  82. */
  83. //10页(含)以下
  84. $currnt="";
  85. if($this->totalPagesCount<=10)
  86. {

  87. for($i=1;$i<=$this->totalPagesCount;$i++)

  88. {

  89. if($i==$this->pageIndex)
  90. { $currnt=" class='current'";}
  91. else
  92. { $currnt=""; }
  93. $str .="$i"."n" ;
  94. }
  95. }else //10页以上
  96. { if($this->pageIndex<3) //当前页小于3
  97. {
  98. for($i=1;$i<=3;$i++)
  99. {
  100. if($i==$this->pageIndex)
  101. { $currnt=" class='current'";}
  102. else
  103. { $currnt=""; }
  104. $str .="$i"."n" ;
  105. }

  106. $str.="……"."n";

  107. for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1

  108. {
  109. $str .="$i"."n" ;

  110. }

  111. }elseif($this->pageIndex<=5) // 5 >= 当前页 >= 3
  112. {
  113. for($i=1;$i<=($this->pageIndex+1);$i++)
  114. {
  115. if($i==$this->pageIndex)
  116. { $currnt=" class='current'";}
  117. else
  118. { $currnt=""; }
  119. $str .="$i"."n" ;

  120. }

  121. $str.="……"."n";

  122. for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1

  123. {
  124. $str .="$i"."n" ;
  125. }

  126. }elseif(5<$this->pageIndex && $this->pageIndex<=$this->totalPagesCount-5 ) //当前页大于5,同时小于总页数-5

  127. {

  128. for($i=1;$i<=3;$i++)

  129. {
  130. $str .="$i"."n" ;
  131. }
  132. $str.="……";
  133. for($i=$this->pageIndex-1 ;$i<=$this->pageIndex+1 && $i<=$this->totalPagesCount-5+1;$i++)
  134. {
  135. if($i==$this->pageIndex)
  136. { $currnt=" class='current'";}
  137. else
  138. { $currnt=""; }
  139. $str .="$i"."n" ;
  140. }
  141. $str.="……";

  142. for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)

  143. {
  144. $str .="$i"."n" ;

  145. }

  146. }else
  147. {

  148. for($i=1;$i<=3;$i++)

  149. {
  150. $str .="$i"."n" ;
  151. }
  152. $str.="……"."n";

  153. for($i=$this->totalPagesCount-5;$i<=$this->totalPagesCount;$i++)//功能1

  154. {
  155. if($i==$this->pageIndex)
  156. { $currnt=" class='current'";}
  157. else
  158. { $currnt=""; }
  159. $str .="$i"."n" ;

  160. }

  161. }
  162. }

  163. /*

  164. 除首末后 页面分页逻辑结束
  165. */
  166. //下一页 末页
  167. if($this->pageIndex==$this->totalPagesCount)
  168. {
  169. $str .="n"."下一页"."n" ;
  170. $str .="末页"."n";

  171. }else
  172. {
  173. $str .="n"."下一页 "."n";
  174. $str .="末页 "."n" ;
  175. }

  176. $str .= "

";
  • return $str;
  • }

  • /**

  • * Get instance
  • * @return
  • */
  • // static public function getInstance() {
  • // if (is_null ( self::$_instance )) {
  • // self::$_instance = new pager ();
  • // }
  • // return self::$_instance;
  • // }

  • }
  • ?>

  • 复制代码

    3),php分页样式文件 pager.css:

    1. body,html{ padding:0px; margin:0px; color:#333333; font-family:"宋体",Arial,Lucida,Verdana,Helvetica,sans-serif; font-size:12px; line-height:150%;}

    2. h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label{margin:0; padding:0; border:none; list-style-type:none;}

    3. /**Front page pagination style**/

    4. .Pagination {margin:10px 0 0;padding:5px 0;text-align:rightright; height:20px; line-height:20px; font-family:Arial, Helvetica, sans-serif,"宋体";}
    5. .Pagination a {margin-left:2px;padding:2px 7px 2px;}
    6. .Pagination .dot{ border:medium none; padding:4px 8px}
    7. .Pagination a:link, .Pagination a:visited {border:1px solid #dedede;color:#696969;text-decoration:none;}
    8. .Pagination a:hover, .Pagination a:active, .Pagination a.current:link, .Pagination a.current:visited {border:1px solid #dedede;color:#fff; background-color:#ff6600; background-image:none; border:#ff6600 solid 1px;}
    9. .Pagination .selectBar{ border:#dedede solid 1px; font-size:12px; width:95px; height:21px; line-height:21px; margin-left:10px; display:inline}
    10. .Pagination a.tips{_padding:4px 7px 1px;}

    复制代码


    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