仿百度分页类

 1 <?php
 2 class Page
 3 {
 4     private $maxnum; //总数据条数
 5     private $num; //每页显示数条数
 6     private $page;//当前页
 7     private $pagenum; //总页数
 8     private $url; //url
 9     private $urlparam; //url参数
10     private $startpage; //当前显示的最左页, 显示页码的区间
11     private $endpage; //当前显示的最右页
12     private $str = ''; //注意成员属性变量和方法内的普通变量的区别,作用域不同的,如果用普通变量,两个方法里的字符串没办法合到一起
13 
14     public function __construct($maxnum, $num)
15     {
16         $this->maxnum = $maxnum;
17         $this->num = $num;
18         $this->pagenum = ceil($this->maxnum/$this->num);
19         $this->page = isset($_GET['page']) ? $_GET['page'] : 1;
20         $this->url = $_SERVER['PHP_SELF']; //directory + filename
21         $this->urlparam();
22         $this->checkpage();
23         // $this->pagemax();
24     }
25 
26     public function urlparam()
27     {
28         foreach($_GET as $k=>$v){
29             if($v != '' && $k != 'page'){
30                 $this->urlparam .= $k.'='.$v.'&';
31             }
32         }
33     }
34 
35     public function showpage()
36     {
37         // $str = '';
38         $this->str .= '当前第'.$this->page.'页,共'.$this->pagenum.'页';
39         $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page=1">首页</a>';
40         $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.($this->page-1).'">上一页</a>';
41         $this->pages();
42         $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.($this->page+1).'">下一页</a>';
43         $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.$this->pagenum.'">尾页</a>';
44         return $this->str;
45     }
46 
47     //当前显示页码的区间
48     //...11 12 13 14 15 16 ...仿百度分页效果
49     public function pages()
50     {
51         if($this->page <= 10){
52             $this->startpage = 1;
53         }else{
54         //通过当前页除以10,在和1拼接,比如当前页15,经过这种方法处理就得到起始页为11.减1的目的是如果当前页是30,那么把他算作21那组 
55             $this->startpage = floor(($this->page-1) / 10).'1'; //这块想要拼接只能使用string类型的1
56         }
57         $this->endpage = $this->startpage + 9; //只显示10个页码
58         if($this->endpage >= $this->pagenum){
59             $this->endpage = $this->pagenum; //
60         }
61         for($i=$this->startpage; $i<=$this->endpage; $i++){
62             //注意这里的变量名必须和showpage()方法里的变量名相同,因为最终是和showpage里的字符串拼接
63             // 还不行,变量作用域不同,即便起相同变量名也不行。只能使用成员属性变量的方式了
64             if($this->page == $i)
65                 $this->str .= '<a style="color:red;" href="'.$this->url.'?'.$this->urlparam.'page='.$i.'">'.$i.'</a>&nbsp;';
66             else
67                 $this->str .='<a href="'.$this->url.'?'.$this->urlparam.'page='.$i.'">'.$i.'</a>';
68         } 
69         return $this->str;  //单独把这个写成一个方法就是为了维护,如果用百度分页效果就调用,不需要就不调用
70     }
71 
72     public function checkpage()
73     {
74         if($this->page >= $this->pagenum){
75             $this->page = $this->pagenum;
76         }
77         if($this->page <= 1){
78             $this->page = 1;
79         }
80     }
81 
82     public function limit()
83     {
84         $num = ($this->page-1) * $this->num;
85         $limit = $num.','.$num;
86         return $limit;
87     }
88 }
89 
90 $p = new page(90, 3);
91 echo $p->showpage();

部分内容参考:https://www.cnblogs.com/liangzia/p/10438263.html

猜你喜欢

转载自www.cnblogs.com/bneglect/p/11129373.html
今日推荐