首頁  >  文章  >  後端開發  >  PHP分頁類的使用

PHP分頁類的使用

WBOY
WBOY原創
2016-07-25 09:12:43909瀏覽

PHP分頁類的使用

page.class.php

  1. class Page {
  2. private $total_rows;//資料庫總條數
  3. private $ per_page_rows;//每頁顯示條數
  4. private $limit;
  5. private $uri;
  6. private $total_pages;//總頁數
  7. private $config=array("header"=>"記錄條數","prev"=>"上一頁","next"=>"下一頁","first"=>"首頁","last"=>"尾頁");
  8. private $list_length=8;
  9. public function __construct($total_rows,$per_page_rows=10,$url_args){
  10. $this->total_rows=$total_rows; $this->uri=$this->get_uri($url_args);
  11. $this->page = !empty($_GET['page']) ? $_GET['page'] : 1;
  12. $this->total_pages=ceil($this->total_rows/$this->per_page_rows);
  13. $this->limit=$this->set_limit();
  14. }
  15. private function set_limit () {
  16. return "limit ".($this->page-1)*$this->per_page_rows.",{$this->per_page_rows}";
  17. }
  18. private function get_uri($ url_args) {
  19. $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],"?") ? "" : "?").$url_args;
  20. $parse= parse_url($url);
  21. if (isset($parse['query'])) {
  22. parse_str($parse['query'],$params);//把url字串解析為陣列
  23. unset($params['page']);//刪除數組下標為page的值
  24. $url=$parse['path'].'?'.http_build_query($params);//再次建構url
  25. }
  26. return $url;
  27. }
  28. public function __get($args) {
  29. if ($args=="limit") {
  30. return $this-> limit;
  31. }else{
  32. return null;
  33. }
  34. }
  35. private function start_page(){
  36. if ($this->total_rows==0) {
  37. return 0;
  38. }else{
  39. return (($this->page-1)*$this->per_page_rows)+1;
  40. }
  41. }
  42. private function end_page(){
  43. return min($this->page*$this->per_page_rows,$this->total_rows);
  44. }
  45. private function go_first() {
  46. $html="";
  47. if ($this->page==1) {
  48. $html.=" {$this->config['first']} ";
  49. }else{
  50. $html.=" {$this->config['first']} ";
  51. }
  52. return $html;
  53. }
  54. private function go_prev() {
  55. $html="";
  56. if ($this->page==1) {
  57. $html.=" {$this->config['prev']} ";
  58. }else{
  59. $html.=" {$this->config['prev ']} ";
  60. }
  61. return $html;
  62. }
  63. private function go_next() {
  64. $html="";
  65. if ($this- >page==$this->total_pages) {
  66. $html.=" {$this->config['next']} ";
  67. }else{
  68. $html.=" {$this->config['next']} ";
  69. }
  70. return $html ;
  71. }
  72. private function go_last() {
  73. $html="";
  74. if ($this->page==$this->total_pages) {
  75. $html.=" {$this->config['last']} ";
  76. }else{
  77. $html.=" {$this->config['last']} ";
  78. }
  79. return $html;
  80. }
  81. private function go_page() {
  82. return '  ';
  83. }
  84. private function page_list() {
  85. $link_page="";
  86. $i_num=floor($this->list_length/2);
  87. for ($i = $i_num; $i >= 1; $i--) {
  88. $page=$this->page-$ i;
  89. if ($page continue;
  90. }else{
  91. $link_page.=" {$page} ";
  92. }
  93. }
  94. $link_page.=" {$this->page} ";
  95. for ($i = 1; $i $page=$this- >page+$i;
  96. if ($pagetotal_pages) {
  97. $link_page.="  {$page} ";
  98. }else{
  99. 規則;
  100. }
  101. }
  102. 回傳$link_page ;
  103. }
  104. public function out_page($display= array(0,1,2,3,4,5,6,7,8)) {
  105. $display_html='';
  106. $ html[0]=" 共有{$this-> Total_rows}{$this->config['header']} ";
  107. $html[1 ]=" 每頁顯示".($this->end_page()-$this- >start_page()+1)."條,本頁顯示從{$this->start_page()}--{$this->end_page()}{$this->config['header'] } ";
  108. $html[2]=" {$this->頁}/{$this-> Total_pages} b>頁";
  109. $html[3]=$this->go_first();
  110. $html[4]=$this->go_prev();
  111. $html[5 ]=$this->page_list();
  112. $html[6]=$this->go_next();
  113. $html[7]=$this->go_last();
  114. $html[ 8]=$this->go_page();
  115. foreach ($display as $index){
  116. $display_html.=$html[$index];
  117. }
  118. return $display_html;
  119. }
  120. }
  121. ?>
複製程式碼

page_demo.php

  1. header("content-type:text/html;charset=utf-8");
  2. require_once './page.class.php';
  3. require_once '../config/config.db.php';
  4. //資料庫中的總條數:total_rows;
  5. //每一頁顯示的條數:per_page_rows
  6. $sql ="select * from cp_sd_day";
  7. $rt=mysql_query($sql);
  8. $total_rows=mysql_num_rows($rt);
  9. $per_page_rows=10;
  10. $page=new頁($total_rows,$per_page_rows);
  11. $sql="select * from cp_sd_day {$page->limit}";
  12. $rt=mysql_query($sql);
  13. echo '';
  14. echo '
  15. '; while (!!$row=mysql_fetch_assoc ($rt)) { echo ''; echo ''; echo ''; echo '';

    echo '

    ';
    echo '; '; echo ''; } echo '';

    cp_sd_day

    '.$row['date_no'].''.$row[' max_notwin '].''.$row['sum_last_miss'].' '.$row[' last_miss '].''.$row['last_miss_sort'].'
    '.$page->out_page ( 備份(2, 3,4,5,6,7,8)).'
    echo '
';?>
複製程式碼


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