Super easy to use php paging class and calling method

WBOY
Release: 2016-07-25 08:52:22
Original
922 people have browsed it
  1. /**

  2. * php paging code
  3. * edit: bbs.it-home.org
  4. */
  5. class Page {
  6. private $total; //Total records
  7. private $pagesize;//How many records are displayed per page
  8. private $limit; //limit
  9. private $page; //Current page number
  10. private $pagenum; //Total page number
  11. private $url; //Address
  12. private $bothnum; //The amount of digital paging on both sides
  13. //Constructor initialization

  14. public function __construct($_total, $_pagesize) {
  15. $this->total = $_total ? $_total : 1;
  16. $this->pagesize = $_pagesize;
  17. $this->pagenum = ceil($this->total / $this->pagesize);
  18. $this->page = $this->setPage();
  19. $this->limit = " LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
  20. $this->url = $this->setUrl();
  21. $ this->bothnum = 2;
  22. }
  23. //Interceptor

  24. private function __get($_key) {
  25. return $this->$_key;
  26. }
  27. //Get the current page number

  28. private function setPage() {
  29. if (!empty($_GET['page'])) {
  30. if ($_GET['page'] > 0) {
  31. if ($_GET['page'] > $this->pagenum) {
  32. return $this->pagenum;
  33. } else {
  34. return $_GET['page'];
  35. }
  36. } else {
  37. return 1 ;
  38. }
  39. } else {
  40. return 1;
  41. }
  42. }
  43. //Get address

  44. private function setUrl() {
  45. $_url = $_SERVER["REQUEST_URI"];
  46. $ _par = parse_url($_url);
  47. if (isset($_par['query'])) {
  48. parse_str($_par['query'],$_query);
  49. unset($_query['page']);
  50. $_url = $_par['path'].'?'.http_build_query($_query);
  51. }
  52. return $_url;
  53. } //Digital directory
  54. private function pageList() {
  55. for ($i=$this ->bothnum;$i>=1;$i--) {
  56. $_page = $this->page-$i;
  57. if ($_page $_pagelist .= ' url.'&page='.$_page.'">'.$_page.' ';
  58. }
  59. $_pagelist .= ' '.$this->page.' ';
  60. for ($i=1;$ibothnum;$i++) {
  61. $_page = $this->page+$i;
  62. if ($_page > $this->pagenum) break;
  63. $_pagelist .= ' '.$_page.' ';
  64. }
  65. return $_pagelist;
  66. }
  67. //Homepage

  68. private function first() {
  69. if ($this->page > $this->bothnum+1) {
  70. return ' 1 .. .';
  71. }
  72. }
  73. //Previous page

  74. private function prev() {
  75. if ($this->page == 1) {
  76. return 'Previous page';
  77. }
  78. return ' Previous page ';
  79. }
  80. //Next page

  81. private function next() {
  82. if ($this->page = = $this->pagenum) {
  83. return 'next page';
  84. }
  85. return ' Next page ';
  86. }
  87. //Last page

  88. private function last() {
  89. if ($this->pagenum - $this->page > $this->bothnum) {
  90. return ' ...'.$this->pagenum.' ';
  91. }
  92. }
  93. / /Paging information

  94. public function showpage() {
  95. $_page .= $this->first();
  96. $_page .= $this->pageList();
  97. $_page .= $this->last( );
  98. $_page .= $this->prev();
  99. $_page .= $this->next();
  100. return $_page;
  101. }
  102. }
  103. ?>
Copy code

Pagination style:

Super easy to use php paging class and calling method

Instructions for use:

  1. $_page = new Page($_total,$_pagesize); //Where $_total is the total number of items in the data set, $_pagesize is the number displayed on each page.
  2. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!