189 8069 5689

PHP分页类高洛峰细说PHP

分页类

创新互联-专业网站定制、快速模板网站建设、高性价比崇信网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式崇信网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖崇信地区。费用合理售后完善,10多年实体公司更值得信赖。

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 'url.'&page=1">首页  url.'&page='.$pre.'">上一页';
                        }else{
                            return '';
                        }                       
            }
            
            //列表
            private function flist(){
                        $list       =   "";
                        $num    =   4;
                        //当前页之前的设置
                        for($i=$num;$i >=1;$i--){
                            $page   =   $this->cpage-$i;
                            if($page >1){
                                $list      .=  ' url.'&page='.$page.'">'.$page.' ';
                            }
                        }
                        //当前页的设置
                        if($this->pages>1){
                            $list   .=' '.$this->cpage.' ';
                        }
                        //当前页之后的设置
                        for($i=1;$i <=$num;$i++){
                            $page   =   $this->cpage+$i;
                            if($page <= $this->pages){
                                $list      .=  ' url.'&page='.$page.'">'.$page.' ';
                            }else{
                                break;
                            }                            
                        }
                        return $list;
            }
            
            //末页
            private function last(){
                if($this->cpage < $this->pages){
                    $next    =   $this->cpage+1;
                    return 'url.'&page='.$next.'">下一页  url.'&page='.$this->pages.'">末页 ';
                }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]=" 共{$this->total}条记录 ";
                        $pages[1]=" 本页显示".$this->currnum()."条记录 ";
                        $pages[2]=" 从".$this->start()."-".$this->end()."条 ";
                        $pages[3]=" {$this->cpage}/{$this->pages} ";
                        $pages[4]=" ".$this->first()." ";
                        $pages[5]=" ".$this->flist()." ";
                        $pages[6]=" ".$this->last()." ";
                        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);

网站栏目:PHP分页类高洛峰细说PHP
链接URL:http://cdxtjz.com/article/jjopop.html

其他资讯