Rumah  >  Artikel  >  pembangunan bahagian belakang  >  一个简单的php分页类代码

一个简单的php分页类代码

WBOY
WBOYasal
2016-07-25 08:43:27925semak imbas
  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.="  {$this->config["first"]}  ";
  55. return $html;
  56. }
  57. private function prev(){
  58. if($this->page==1)
  59. $html.='';
  60. else
  61. $html.="  {$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 continue;
  70. $linkPage.=" {$page} ";
  71. }
  72. $linkPage.=" {$this->page} ";
  73. for($i=1; $i $page=$this->page+$i;
  74. if($pagepageNum)
  75. $linkPage.=" {$page} ";
  76. else
  77. break;
  78. }
  79. return $linkPage;
  80. }
  81. private function next(){
  82. if($this->page==$this->pageNum)
  83. $html.='';
  84. else
  85. $html.="  {$this->config["next"]}  ";
  86. return $html;
  87. }
  88. private function last(){
  89. if($this->page==$this->pageNum)
  90. $html.='';
  91. else
  92. $html.="  {$this->config["last"]}  ";
  93. return $html;
  94. }
  95. private function goPage(){
  96. return '    ';
  97. }
  98. function fpage($display=array(0,1,2,3,4,5,6,7,8)){
  99. $html[0]="  共有{$this->total}{$this->config["header"]}  ";
  100. $html[1]="  每页显示".($this->end()-$this->start()+1)."条,本页{$this->start()}-{$this->end()}条  ";
  101. $html[2]="  {$this->page}/{$this->pageNum}页  ";
  102. $html[3]=$this->first();
  103. $html[4]=$this->prev();
  104. $html[5]=$this->pageList();
  105. $html[6]=$this->next();
  106. $html[7]=$this->last();
  107. $html[8]=$this->goPage();
  108. $fpage='';
  109. foreach($display as $index){
  110. $fpage.=$html[$index];
  111. }
  112. return $fpage;
  113. }
  114. }
复制代码

分页, php


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn