Home >Backend Development >PHP Tutorial >PHP general paging class code, imitating Google paging style

PHP general paging class code, imitating Google paging style

WBOY
WBOYOriginal
2016-07-25 08:52:371150browse
  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->page<=10){
  55. for ($i=1;$i<$this->page+1;$i++){
  56. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  57. }
  58. }else{
  59. if ($this->pg<=5){
  60. for ($i=1;$i<10;$i++){
  61. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  62. }
  63. }else{
  64. if (6+$this->pg<=$this->page){
  65. for ($i=$this->pg-4;$i<$this->pg+6;$i++){
  66. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  67. }
  68. }else{
  69. for ($i=$this->pg-4;$i<$this->page+1;$i++){
  70. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  71. }

  72. }

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

复制代码


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