Heim  >  Artikel  >  Backend-Entwicklung  >  PHP分页类的使用

PHP分页类的使用

WBOY
WBOYOriginal
2016-07-25 09:12:43858Durchsuche

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.=" {$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.=" {$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.=" {$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.=" {$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 continue;
  92. }else{
  93. $link_page.=" {$page} ";
  94. }
  95. }
  96. $link_page.=" {$this->page} ";
  97. for ($i = 1; $i $page=$this->page+$i;
  98. if ($pagetotal_pages) {
  99. $link_page.=" {$page} ";
  100. }else{
  101. break;
  102. }
  103. }
  104. return $link_page;
  105. }
  106. public function out_page($display=array(0,1,2,3,4,5,6,7,8)) {
  107. $display_html='';
  108. $html[0]=" 共有{$this->total_rows}{$this->config['header']} ";
  109. $html[1]=" 每页显示".($this->end_page()-$this->start_page()+1)."条,本页显示从{$this->start_page()}--{$this->end_page()}{$this->config['header']} ";
  110. $html[2]=" {$this->page}/{$this->total_pages}页 ";
  111. $html[3]=$this->go_first();
  112. $html[4]=$this->go_prev();
  113. $html[5]=$this->page_list();
  114. $html[6]=$this->go_next();
  115. $html[7]=$this->go_last();
  116. $html[8]=$this->go_page();
  117. foreach ($display as $index){
  118. $display_html.=$html[$index];
  119. }
  120. return $display_html;
  121. }
  122. }
  123. ?>
复制代码

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. ';
  16. while (!!$row=mysql_fetch_assoc($rt)) {
  17. echo '
  18. ';
  19. echo '
  20. ';
  21. echo '
  22. ';
  23. echo '
  24. ';
  25. echo '
  26. ';
  27. echo '
  28. ';
  29. echo '
  30. ';
  31. }
  32. echo '
  33. ';
  34. echo '
  35. 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)).'
    ';
  36. ?>
复制代码




Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn