search
HomeBackend DevelopmentPHP TutorialPHP general paging class code, imitating Google paging style

  1. /**

  2. ** General PHP paging class. (Imitating Google style)
  3. ** Just provide two parameters: the total number of records and the number of displays per page. (Detailed instructions are attached.)
  4. ** There is no need to specify the URL, the link is generated by the program. Convenient for paging search results.
  5. ** The form is submitted using the GET method, which ensures that URL parameters are not lost during operations such as query and deletion.
  6. **/
  7. class Pager{
  8. //IE address bar address
  9. var $url;
  10. //Total number of records
  11. var $countall ;
  12. //Total number of pages
  13. var $page;
  14. //Paging number link
  15. var $thestr;
  16. //Homepage, previous page link
  17. var $backstr;
  18. //Last page, next page link
  19. var $ nextstr;
  20. //Current page number
  21. var $pg;
  22. //Number of records displayed on each page
  23. var $countlist;
  24. //Page turning style
  25. var $style;
  26. //Constructor, automatically executed when instantiating this class This function
  27. function Pager($countall,$countlist,$style="page"){
  28. //When the number of records and the number displayed on each page cannot be integrated, the number of pages will be remaindered by adding 1
  29. $this->countall = $ countall;
  30. $this->countlist = $countlist;
  31. $this->style=$style;
  32. if ($this->countall%$this->countlist!=0){
  33. $this-> ;page=sprintf("%d",$this->countall/$this->countlist)+1;
  34. }else{
  35. $this->page=$this->countall/$this-> ;countlist;
  36. }
  37. $this->pg=$_GET["pg"];

  38. //Guarantee pg starts from page 1 if not specified
  39. if ( !ereg("^[1-9][0-9]*$",$this->pg) || empty($this->pg)){
  40. $this->pg=1;
  41. }
  42. //The page number exceeds the maximum range, take the maximum value
  43. if ($this->pg>$this->page){
  44. $this->pg=$this->page;
  45. }
  46. // Get the current URL. Please see the function entity at the bottom for the specific implementation
  47. $this->url = Pager::getUrl();
  48. //Replace the incorrectly formatted page number with the correct page number
  49. if(isset($_GET["pg"]) && $ _GET["pg"]!=$this->pg){
  50. $this->url=str_replace("?pg=".$_GET["pg"],"?pg=$this->pg ",$this->url);
  51. $this->url=str_replace("&pg=".$_GET["pg"],"&pg=$this->pg",$this->url );
  52. }
  53. //Generate pagination in the form of numbers such as 12345.
  54. if ($this->pagefor ($i=1;$ipage+1;$i++){
  55. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  56. }
  57. }else{
  58. if ($this->pgfor ($i=1;$i$this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  59. }
  60. }else{
  61. if (6+$this->pgpage){
  62. for ($i=$this->pg-4;$ipg+6;$i++){
  63. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  64. }
  65. }else{
  66. for ($i=$this->pg-4;$ipage+1;$i++){
  67. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  68. }
  69. }

  70. }
  71. }
  72. //生成上页下页等文字链接
  73. $this->backstr = Pager::gotoback($this->pg);
  74. $this->nextstr = Pager::gotonext($this->pg,$this->page);
  75. //echo (" 共".$this->countall." 条,每页".$this->countlist."条,共".$this->page."页".$this->backstr.$this->thestr.$this->nextstr);
  76. }
  77. //生成数字分页的辅助函数
  78. function makepg($i,$pg){
  79. if ($i==$pg){
  80. return " ".$i."";
  81. }else{
  82. return " ".$i."";
  83. }
  84. }
  85. //生成上一页等信息的函数
  86. function gotoback($pg){
  87. if ($pg-1>0){
  88. return $this->gotoback=" 首页 上一页";
  89. }else{
  90. return $this->gotoback="首页 上一页 ";
  91. }
  92. }
  93. //生成下一页等信息的函数
  94. function gotonext($pg,$page){
  95. if ($pg return " 下一页 尾页";
  96. }else{
  97. return " 下一页 尾页";
  98. }
  99. } bbs.it-home.org
  100. //处理url中$pg的方法,用于自动生成pg=x
  101. function replacepg($url,$flag,$i){
  102. if ($flag == 1){
  103. $temp_pg = $this->pg;
  104. return str_replace("pg=".$temp_pg,"pg=".($this->pg+1),$url);
  105. }else if($flag == 2) {
  106. $temp_pg = $this->pg;
  107. return str_replace("pg=".$temp_pg,"pg=".($this->pg-1),$url);
  108. }else if($flag == 3) {
  109. $temp_pg = $this->pg;
  110. return str_replace("pg=".$temp_pg,"pg=1",$url);
  111. }else if($flag == 4){
  112. $temp_pg = $this->pg;
  113. return str_replace("pg=".$temp_pg,"pg=".$this->page,$url);
  114. }else if($flag == 5){
  115. $temp_pg = $this->pg;
  116. return str_replace("pg=".$temp_pg,"pg=".$i,$url);
  117. }else{
  118. return $url;
  119. }
  120. }
  121. //获得当前URL的方法
  122. function getUrl(){
  123. $url="http://".$_SERVER["HTTP_HOST"];
  124. if(isset($_SERVER["REQUEST_URI"])){
  125. $url.=$_SERVER["REQUEST_URI"];
  126. }else{
  127. $url.=$_SERVER["PHP_SELF"];
  128. if(!empty($_SERVER["QUERY_STRING"])){
  129. $url.="?".$_SERVER["QUERY_STRING"];
  130. }
  131. }
  132. //在当前的URL里加入pg=x字样
  133. if (!ereg("(pg=|PG=|pG=|Pg=)", $url)){
  134. if (!strpos($url,"?")){
  135. $url = $url."?pg=1";
  136. }else{
  137. $url = $url."&pg=1";
  138. }
  139. }
  140. return $url;
  141. }
  142. }
  143. ?>
复制代码


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 Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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 Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools