"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页"); private $listNum=8; /* * $tot">
当前位置:Gxlcms > PHP教程 > 一个简单的php分页类代码

一个简单的php分页类代码

时间:2021-07-01 10:21:17 帮助过:22人阅读

  1. class Page {
  2. private $total; //数据表中总记录数
  3. private $listRows; //每页显示行数
  4. private $limit;
  5. private $uri;
  6. private $pageNum; //页数
  7. private $config=array('header'=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页");
  8. private $listNum=8;
  9. /*
  10. * $total
  11. * $listRows
  12. */
  13. public function __construct($total, $listRows=10, $pa=""){
  14. $this->total=$total;
  15. $this->listRows=$listRows;
  16. $this->uri=$this->getUri($pa);
  17. $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
  18. $this->pageNum=ceil($this->total/$this->listRows);
  19. $this->limit=$this->setLimit();
  20. }
  21. private function setLimit(){
  22. return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";
  23. }
  24. private function getUri($pa){
  25. $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;
  26. echo $url;
  27. $parse=parse_url($url);
  28. if(isset($parse["query"])){
  29. parse_str($parse['query'],$params);
  30. unset($params["page"]);
  31. $url=$parse['path'].'?'.http_build_query($params);
  32. }
  33. return $url;
  34. }
  35. private function __get($args){
  36. if($args=="limit")
  37. return $this->limit;
  38. else
  39. return null;
  40. }
  41. private function start(){
  42. if($this->total==0)
  43. return 0;
  44. else
  45. return ($this->page-1)*$this->listRows+1;
  46. }
  47. private function end(){
  48. return min($this->page*$this->listRows,$this->total);
  49. }
  50. private function first(){
  51. if($this->page==1)
  52. $html.='';
  53. else
  54. $html.=" uri}&page=1">{$this->config["first"]} ";
  55. return $html;
  56. }
  57. private function prev(){
  58. if($this->page==1)
  59. $html.='';
  60. else
  61. $html.=" uri}&page='.($this->page-1).'">{$this->config["prev"]} ";
  62. return $html;
  63. }
  64. private function pageList(){
  65. $linkPage="";
  66. $inum=floor($this->listNum/2);
  67. for($i=$inum; $i>=1; $i--){
  68. $page=$this->page-$i;
  69. if($page<1)
  70. continue;
  71. $linkPage.=" uri}&page={$page}">{$page} ";
  72. }
  73. $linkPage.=" {$this->page} ";
  74. for($i=1; $i<=$inum; $i++){
  75. $page=$this->page+$i;
  76. if($page<=$this->pageNum)
  77. $linkPage.=" uri}&page={$page}">{$page} ";
  78. else
  79. break;
  80. }
  81. return $linkPage;
  82. }
  83. private function next(){
  84. if($this->page==$this->pageNum)
  85. $html.='';
  86. else
  87. $html.=" uri}&page='.($this->page+1).'">{$this->config["next"]} ";
  88. return $html;
  89. }
  90. private function last(){
  91. if($this->page==$this->pageNum)
  92. $html.='';
  93. else
  94. $html.=" uri}&page='.($this->pageNum).'">{$this->config["last"]} ";
  95. return $html;
  96. }
  97. private function goPage(){
  98. return ' ';
  99. }
  100. function fpage($display=array(0,1,2,3,4,5,6,7,8)){
  101. $html[0]=" 共有{$this->total}{$this->config["header"]} ";
  102. $html[1]=" 每页显示".($this->end()-$this->start()+1)."条,本页{$this->start()}-{$this->end()}条 ";
  103. $html[2]=" {$this->page}/{$this->pageNum}页 ";
  104. $html[3]=$this->first();
  105. $html[4]=$this->prev();
  106. $html[5]=$this->pageList();
  107. $html[6]=$this->next();
  108. $html[7]=$this->last();
  109. $html[8]=$this->goPage();
  110. $fpage='';
  111. foreach($display as $index){
  112. $fpage.=$html[$index];
  113. }
  114. return $fpage;
  115. }
  116. }

分页, php

人气教程排行