Home  >  Article  >  Backend Development  >  Use of PHP paging class

Use of PHP paging class

WBOY
WBOYOriginal
2016-07-25 09:12:43909browse

Usage of PHP paging class

page.class.php

  1. class Page {
  2. private $total_rows;//Total number of entries in the database
  3. private $per_page_rows;//Number of entries displayed per page
  4. private $limit;
  5. private $ uri;
  6. private $total_pages;//Total number of pages
  7. private $config=array("header"=>"Number of records","prev"=>"Previous page","next"=>" Next page","first"=>"Home page","last"=>"Last page");
  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);/ /Parse the url string into an array
  25. unset($params['page']);//Delete the value whose subscript is page in the array
  26. $url=$parse['path'].'?'.http_build_query($params );//Build the url again
  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']}< /a> ";
  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<1) {
  92. continue;
  93. }else{
  94. $link_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.=" {$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. ';
  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. ?>
复制代码




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