Home  >  Article  >  Backend Development  >  A simple php pagination class code

A simple php pagination class code

WBOY
WBOYOriginal
2016-07-25 08:43:27882browse
  1. class Page {
  2. private $total; //Total number of records in the data table
  3. private $listRows; //Number of rows displayed per page
  4. private $limit;
  5. private $uri;
  6. private $ pageNum; //Number of pages
  7. private $config=array('header'=>"records", "prev"=>"previous page", "next"=>"next page", "first "=>"Home page", "last"=>"Last page");
  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<1)
  70. continue;
  71. $linkPage.="  {$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.=" {$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.="  {$this->config["next"]}  ";
  88. return $html;
  89. }
  90. private function last(){
  91. if($this->page==$this->pageNum)
  92. $html.='';
  93. else
  94. $html.="  {$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


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