PHP分頁類的使用

來源:互聯網
上載者:User

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

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 Page($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)) {
  16. echo '
  17. echo '
  18. echo '
  19. echo '
  20. echo '
  21. echo '
  22. }
  23. echo '
  24. '; ';
  25. echo '
  26. '; '; '; '; '; '; ';
  27. echo '
  28. ';

    cp_sd_day

    '.$row['date_no'].''.$row['max_notwin'].''.$row['sum_last_miss'].''.$row['last_miss'].''.$row['last_miss_sort'].'
    '.$page->out_page(array(2,3,4,5,6,7,8)).'
    ';
  29. ?>
複製代碼


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.