PHPページング類の高洛峰はPHPを詳しく話します


ページングクラス
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 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);