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

时间:2016-07-25 08:52:22 来源:互联网 作者: 神秘的大神 字体:

  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 < 1) continue;
  58. $_pagelist .= ' '.$_page.' ';
  59. }
  60. $_pagelist .= ' '.$this->page.' ';
  61. for ($i=1;$i<=$this->bothnum;$i++) {
  62. $_page = $this->page+$i;
  63. if ($_page > $this->pagenum) break;
  64. $_pagelist .= ' '.$_page.' ';
  65. }
  66. return $_pagelist;
  67. }

  68. //首页

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

  74. //上一页

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

  81. //下一页

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

  88. //尾页

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

  94. //分页信息

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

复制代码

分页样式:

php分页类样式

使用说明:

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