温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP 分页类 高洛峰 细说PHP

发布时间:2020-07-26 06:00:13 来源:网络 阅读:644 作者:津沙港湾 栏目:web开发

分页类

<?php 
     //分页类
    class Page{
            private $total;          //总记录数
            private $nums;        //每页显示的条数
            private $pages;       //总页数
            private $cpage;      //当前页
            private $url;           //当前url
            public  function __construct($total,$nums){
                        $this->total      =     $total;
                        $this->nums     =     $nums;
                        $this->pages    =      $this->getPages();
                        $this->url         =      $this->setUrl();
                        //获取当前页
                        $this->cpage   =    !empty($_GET['page']) ? intval($_GET['page']):1;
                        $this->cpage   =    $this->cpage>0 ?$this->cpage:1;
                        $this->cpage   =    $this->cpage>$this->pages?$this->pages:$this->cpage;
            }
            
            private function setUrl(){
                        $url    =   $_SERVER['REQUEST_URI'];
                        if(strstr($url, "?")){
                            $arr = parse_url($url);
                            if(isset($arr['query'])){
                                parse_str($arr['query'],$output);
                            }
                            //删除url中重复的page=n的字符串
                            unset($output['page']);
                            $url = $arr['path']."?".http_build_query($output);
                        }else{
                            $url .="?";
                        }
                        return $url;
            }
            //获取页数
            private function getPages(){
                        return  ceil($this->total/$this->nums);// 向上取整法
            }
            
            //首页
            private function first(){
                        //如果当前页是第一页 则不显示这些
                        if($this->cpage>1){
                            $pre    =   $this->cpage-1;
                            return '<a href="'.$this->url.'&page=1">首页</a>&nbsp;&nbsp;<a href="'.$this->url.'&page='.$pre.'">上一页</a>';
                        }else{
                            return '';
                        }                       
            }
            
            //列表
            private function flist(){
                        $list       =   "";
                        $num    =   4;
                        //当前页之前的设置
                        for($i=$num;$i >=1;$i--){
                            $page   =   $this->cpage-$i;
                            if($page >1){
                                $list      .=  '&nbsp;<a href="'.$this->url.'&page='.$page.'">'.$page.'</a>&nbsp;';
                            }
                        }
                        //当前页的设置
                        if($this->pages>1){
                            $list   .='&nbsp;'.$this->cpage.'&nbsp;';
                        }
                        //当前页之后的设置
                        for($i=1;$i <=$num;$i++){
                            $page   =   $this->cpage+$i;
                            if($page <= $this->pages){
                                $list      .=  '&nbsp;<a href="'.$this->url.'&page='.$page.'">'.$page.'</a>&nbsp;';
                            }else{
                                break;
                            }                            
                        }
                        return $list;
            }
            
            //末页
            private function last(){
                if($this->cpage < $this->pages){
                    $next    =   $this->cpage+1;
                    return '<a href="'.$this->url.'&page='.$next.'">下一页</a>&nbsp;&nbsp;<a href="'.$this->url.'&page='.$this->pages.'">末页</a> ';
                }else{
                    return '';
                }
                      
            }
            //开始    从多少条记录开始
            private function start(){
                        return  ($this->cpage-1)*$this->nums+1;
            }
            //结束    到多少条记录结束
            private function end(){
                        return  min($this->cpage*$this->nums,$this->total);
            }
            //当前显示的条数
            private function currnum(){
                        return $this->end()-$this->start()+1;
            }
            //分页方法
            public function fpage(){
                
                        $arr =  func_get_args();
                        
                        $fpage ="";
                        $pages[0]="&nbsp;共{$this->total}条记录&nbsp;";
                        $pages[1]="&nbsp;本页显示".$this->currnum()."条记录&nbsp;";
                        $pages[2]="&nbsp;从".$this->start()."-".$this->end()."条&nbsp;";
                        $pages[3]="&nbsp;{$this->cpage}/{$this->pages}&nbsp;";
                        $pages[4]="&nbsp;".$this->first()."&nbsp;";
                        $pages[5]="&nbsp;".$this->flist()."&nbsp;";
                        $pages[6]="&nbsp;".$this->last()."&nbsp;";
                        if(count($arr)<1)
                        $arr    =   array(0,1,2,3,4,5,6);
                        foreach($arr as $n){
                                $fpage  .=  $pages[$n];
                        }
                        return $fpage;
        }
        
    }

调用分页类

        include 'print.php';
        $page = new Page(1000,10);
       echo $page->fpage(6,4,5);


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI