欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

超好用php分页类及调用方法

程序员文章站 2022-05-18 14:56:58
...
  1. /**

  2. * php分页类代码
  3. * edit: bbs.it-home.org
  4. */
  5. class Page {
  6. private $total; //总记录
  7. private $pagesize;//每页显示多少条
  8. private $limit; //limit
  9. private $page; //当前页码
  10. private $pagenum; //总页码
  11. private $url; //地址
  12. private $bothnum; //两边保持数字分页的量
  13. //构造方法初始化

  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. //拦截器

  24. private function __get($_key) {
  25. return $this->$_key;
  26. }
  27. //获取当前页码

  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. //获取地址

  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. } //数字目录
  54. private function pageList() {
  55. for ($i=$this->bothnum;$i>=1;$i--) {
  56. $_page = $this->page-$i;
  57. if ($_page $_pagelist .= ' '.$_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. //首页

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

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

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

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

  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. ?>
复制代码

分页样式:

超好用php分页类及调用方法

使用说明:

  1. $_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.
  2. ?>
复制代码