当前位置:Gxlcms > PHP教程 > php分页类代码与分页调用截图

php分页类代码与分页调用截图

时间:2021-07-01 10:21:17 帮助过:5人阅读

  1. class Page {

  2. private $total; //总记录
  3. private $pagesize;//每页显示多少条
  4. private $limit; //limit
  5. private $page; //当前页码
  6. private $pagenum; //总页码
  7. private $url; //地址
  8. private $bothnum; //两边保持数字分页的量

  9. //构造方法初始化

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

  19. //拦截器

  20. private function __get($_key) {
  21. return $this->$_key;
  22. }

  23. //获取当前页码

  24. private function setPage() {
  25. if (!empty($_GET['page'])) {
  26. if ($_GET['page'] > 0) {
  27. if ($_GET['page'] > $this->pagenum) {
  28. return $this->pagenum;
  29. } else {
  30. return $_GET['page'];
  31. }
  32. } else {
  33. return 1;
  34. }
  35. } else {
  36. return 1;
  37. }
  38. }

  39. //获取地址

  40. private function setUrl() {
  41. $_url = $_SERVER["REQUEST_URI"];
  42. $_par = parse_url($_url);
  43. if (isset($_par['query'])) {
  44. parse_str($_par['query'],$_query);
  45. unset($_query['page']);
  46. $_url = $_par['path'].'?'.http_build_query($_query);
  47. }
  48. return $_url;
  49. }

  50. //数字目录

  51. private function pageList() {
  52. for ($i=$this->bothnum;$i>=1;$i--) {
  53. $_page = $this->page-$i;
  54. if ($_page < 1) continue;
  55. $_pagelist .= ' url.'&page='.$_page.'">'.$_page.' ';
  56. }
  57. $_pagelist .= ' '.$this->page.' ';
  58. for ($i=1;$i<=$this->bothnum;$i++) {
  59. $_page = $this->page+$i;
  60. if ($_page > $this->pagenum) break;
  61. $_pagelist .= ' url.'&page='.$_page.'">'.$_page.' ';
  62. }
  63. return $_pagelist;
  64. }

  65. //首页

  66. private function first() {
  67. if ($this->page > $this->bothnum+1) {
  68. return ' url.'">1 ...';
  69. }
  70. }

  71. //上一页

  72. private function prev() {
  73. if ($this->page == 1) {
  74. return '上一页';
  75. }
  76. return ' url.'&page='.($this->page-1).'">上一页 ';
  77. }

  78. //下一页

  79. private function next() {
  80. if ($this->page == $this->pagenum) {
  81. return '下一页';
  82. }
  83. return ' url.'&page='.($this->page+1).'">下一页 ';
  84. }

  85. //尾页

  86. private function last() {
  87. if ($this->pagenum - $this->page > $this->bothnum) {
  88. return ' ...url.'&page='.$this->pagenum.'">'.$this->pagenum.' ';
  89. }
  90. }

  91. //分页信息

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

2,php分页类调用示例

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

3,php分页效果截图 php分页类代码

人气教程排行