首頁  >  文章  >  後端開發  >  一個簡單的php分頁類別程式碼

一個簡單的php分頁類別程式碼

WBOY
WBOY原創
2016-07-25 08:43:27925瀏覽
  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(){ if( $this->page==$this->pageNum)
  82. $html.='';
  83. else
  84. $html.="  {$this->config["next"]}  ";
  85. return $html;
  86. }
  87. private function last(){
  88. if($this->page==$this->pageNum)
  89. $html.='';
  90. else
  91. $ html. =" {$this->config["last" ]}  ";
  92. return $html;
  93. }
  94. private function goPage(){
  95. return '  page.'" >  ';
  96. }
  97. 函數fpage($display=array(0,1,2,3,4,5,6,7,8)) {
  98. $html[0]="  共用{ $this->total}{$this->config["header"]}  ";
  99. $html[1]="每頁顯示".($this->end()-$this->start() 1)."條,本頁{$this->start()}- { $this->end()}條  ";
  100. $html[2]="  {$this->頁面}/{ $this->pageNum}頁  ";
  101. $html[3]=$this->first();
  102. $html[4]=$this->prev();
  103. $html[5]=$this-> pageList();
  104. $html[6]=$this->next();
  105. $html[ 7]=$this->last();
  106. $html[8]=$this- >goPage();
  107. $fpage='';
  108. foreach($顯示為$index) {
  109. $fpage.=$html[$index];
  110. }
  111. return $fpage;
  112. }
  113. }
複製程式碼

分頁, php


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn