当前位置:网站首页 > 软件教程 > 正文

实例讲述PHP实现简单实用的分页类

作者:admin发布时间:2021-11-02分类:软件教程浏览:评论:0


导读:本文实例讲述了PHP实现简单实用的分页类。分享给大家供大家参考,具体如下:12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
文中案例叙述了PHP完成非常简单好用的分页查询类。共享给大伙儿供各位参照,实际方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 <?php
  class Page {
     private $total;   //总纪录
     private $pagesize;  //每张表明是多少条
     private $limit;     //limit
     private $page;      //当今页数
     private $pagenum;   //总页数
     private $url;      //详细地址
     private $bothnum;   //两侧维持数据分页查询的量
   //构造函数复位
   public function ._construct($_total, $_pagesize) {
     $this->total = $_total ? $_total : 1;
     $this->pagesize = $_pagesize;
     $this->pagenum = ceil($this->total / $this->pagesize);
     $this->page = $this->setPage();
     $this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
     $this->url = $this->setUrl();
     $this->bothnum = 2;
   }
   //回调函数
   private function ._get($_key) {
     return $this->$_key;
   }
   //获得当今页数
   private function setPage() {
     if (!empty($_GET['page'])) {
        if ($_GET['page'] > 0) {
          if ($_GET['page'] > $this->pagenum) {
             return $this->pagenum;
          } else {
             return $_GET['page'];
          }
        } else {
          return 1;
        }
     } else {
        return 1;
     }
   }
   //获得详细地址
   private function setUrl() {
     $_url = $_SERVER["REQUEST_URI"];
     $_par = parse_url($_url);
     if (isset($_par['query'])) {
        parse_str($_par['query'],$_query);
        unset($_query['page']);
        $_url = $_par['path'].'?'.http_build_query($_query);
     }
     return $_url;
   }   //数据文件目录
   private function pageList() {
     for ($i=$this->bothnum;$i>=1;$i--) {
      $_page = $this->page-$i;
      if ($_page < 1) continue;
        $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';
     }
     $_pagelist .= ' <span class="me">'.$this->page.'</span> ';
     for ($i=1;$i<=$this->bothnum;$i ) {
      $_page = $this->page $i;
        if ($_page > $this->pagenum) break;
        $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';
     }
     return $_pagelist;
   }
   //主页
   private function first() {
     if ($this->page > $this->bothnum 1) {
        return ' <a href="'.$this->url.'">1</a> ...';
     }
   }
   //上一页
   private function prev() {
     if ($this->page == 1) {
        return '<span class="disabled">上一页</span>';
     }
     return ' <a href="'.$this->url.'&page='.($this->page-1).'">上一页</a> ';
   }
   //下一页
   private function next() {
     if ($this->page == $this->pagenum) {
        return '<span class="disabled">下一页</span>';
     }
     return ' <a href="'.$this->url.'&page='.($this->page 1).'">下一页</a> ';
   }
   //末页
   private function last() {
     if ($this->pagenum - $this->page > $this->bothnum) {
        return ' ...<a href="'.$this->url.'&page='.$this->pagenum.'">'.$this->pagenum.'</a> ';
     }
   }
   //分页查询信息内容
   public function showpage() {
     $_page .= $this->first();
     $_page .= $this->pageList();
     $_page .= $this->last();
     $_page .= $this->prev();
     $_page .= $this->next();
     return $_page;
   }
 }
?>
分页样式如下图所显示:
实例讲述了PHP实现简单实用的分页类
使用说明书:
1
2
3
4 <?php
  $_page = new Page($_total,$_pagesize);
  //在其中 $_total 是数据的总总数,$_pagesize 是每张表明的总数.
?>

标签:PHP


欢迎 发表评论: