php分頁類

lanyu發表於2021-09-09

//分頁類

 

class page{

    private $total_rows; //總條數

    private $onepage_rows;//一頁顯示數量

    private $total_page; //總頁數

    private $page_rows; //頁碼數量

    private $start_id;//描述起始ID

    private $end_id;//描述結束ID

    private $desc = ''; //描述上一頁,下一頁

    private $self_page; //當前頁

    private $url;

 

    public function __construct($total,$onepage_rows=10,$page_rows=5,$desc=''){

        $this->total_rows = $total;

        $this->onepage_rows = $onepage_rows;

        $this->page_rows = $page_rows;

        $this->total_page = ceil($this->total_rows / $this->onepage_rows);

        $this->self_page = min(max((int)@$_GET['page'],1),$this->total_page);

        $this->start_id= ($this->self_page-1)*$this->onepage_rows+1;

        $this->end_id= min($this->self_page*$this->onepage_rows , $this->total_rows);

        $this->desc = $this->desc($desc); //上一頁 下一頁 共X條記錄等資訊

        $this->url = $this->requestUrl(); //URL生成

    }

 

    private function requestUrl(){

        $url =isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'] ;

        $parse_url = parse_url($url);

        if(isset($parse_url['query'])){

            parse_str($parse_url['query'],$arr);

            unset($arr['page']);

            $url =  $parse_url['path'] .'?'.http_build_query($arr).'&page=';

        }else{

            $url = strstr($url,'?') ?  $url.'page=' :$url. '?page=';

        }    

        return $url;

    }

 

    //指定列表頁碼

    public function pagelist(){  

        $start=max(1,min($this->self_page-ceil($this->page_rows/2),$this->total_page-$this->page_rows));

        $end=$start + $this->page_rows;

        $arr = array();

        for($i=$start; $i

            if($i == $this->self_page){

                $arr[$i]['url'] = '';

                $arr[$i]['str'] = $i;

                continue;

            }

            $arr[$i]['url'] = $this->url.$i;

            $arr[$i]['str'] = $i;

        }

        return $arr;

    }

 

    //返回程式碼列表的字串表示

    public function pageshow(){

        $pagelist = $this->pagelist();

        $pagestr = '';

        foreach($pagelist as $v){

            if(empty($v['url'])){

                $pagestr .= ''.$v['str'].'';

                continue;

            }

            $pagestr .= "':'';

    }     

    //末頁

    public function last(){

        return $this->self_page page_rows ? '':'';

    }

 

    //上一頁

    public function prev(){

        return $this->self_page > 1 ? '':'';

    }

    //下一頁

    public function next(){

        return $this->self_page total_page ? '':'';

    }

    //返回當前第X-X條

    public function nowpage(){

        return '當前第'.$this->self_page.'頁 第'.$this->start_id.'——'.$this->end_id.'條';

    }

 

    public function limit(){

        return 'limit '.max(0,($this->self_page-1)*$this->onepage_rows).','.$this->onepage_rows;

    }

}

 ?>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2035/viewspace-2802224/,如需轉載,請註明出處,否則將追究法律責任。

相關文章