本文實例為大家分享了php分頁類的具體代碼,供大家參考,具體內(nèi)容如下
<?php /*核心:首頁、上一頁、下一頁、尾頁的url*/ /*超全局$_SERVER*/ $page = new Page(5,60); var_dump($page->allUrl()); class Page{ // 每頁顯示的個數(shù) protected $number; // 一共有多少數(shù)據(jù) protected $totalCount; // 當(dāng)前頁 protected $page; // url protected $url; public function __construct($number,$totalCount){ $this->number= $number; $this->totalCount = $totalCount; //得到總頁數(shù) $this->totalPage = $this->getTotalPage(); //得到當(dāng)前頁數(shù) $this->page = $this->getPage(); //得到URL $this->url = $this->getUrl(); echo $this->url; } /*得到總頁數(shù)并向上取整*/ protected function getTotalPage(){ return ceil($this->totalCount/$this->number); } /**/ protected function getPage(){ if (empty($_GET['page'])){ $page=1; }elseif ($_GET['page'] > $this->totalPage){ $page = $this->totalPage; }elseif ($_GET["page"]<1){ $page = 1; }else{ $page = $_GET['page']; } return $page; } protected function getUrl(){ //得到協(xié)議名 $scheme = $_SERVER['REQUEST_SCHEME']; //得到主機(jī)名 $host= $_SERVER['SERVER_NAME']; //得到端口號 $port = $_SERVER['SERVER_PORT']; //得到路徑和請求字符串 $url = $_SERVER['REQUEST_URI']; /*中間做處理,要將page=5等這種字符串拼接URL 中,所以如果原來的url中有page這個參數(shù),我們首先 需要將原來的page參數(shù)給清空*/ $urlArray = parse_url($url); // var_dump($urlArray); $path = $urlArray['path']; if (!empty($urlArray['query'])){ //將query中的值轉(zhuǎn)化為數(shù)組 parse_str($urlArray['query'],$array); //如果他有page就將它刪掉 unset($array['page']); //將關(guān)聯(lián)數(shù)組轉(zhuǎn)化為query $query = http_build_query($array); //不為空的話就與path連結(jié) if ($query != ''){ $path = $path.'?'.$query; } } return 'http://'. $host.':'.$port.$path; } protected function setUrl($str){ if (strstr($this->url, '?')){ $url = $this->url.'&'.$str; }else{ $url = $this->url.'?'.$str; } return $url; } /*所有的url*/ public function allUrl(){ return [ 'first' => $this->first(), 'next' => $this->next(), 'prev'=> $this->prev(), 'end'=> $this->end(), ]; } /*首頁*/ public function first(){ return $this->setUrl('page=1'); } /*下一頁*/ public function next(){ //根據(jù)當(dāng)前page得帶下一頁的頁碼 if ($this->page+1 > $this->totalPage) { $page = $this->totalPage; }else{ $page = $this->page+1; } return $this->setUrl('page='.$page); } /*上一頁*/ public function prev(){ //根據(jù)當(dāng)前page得帶下一頁的頁碼 if ($this->page - 1 < 1) { $page = 1; }else{ $page = $this->page-1; } return $this->setUrl('page='.$page); } /*尾頁*/ public function end(){ return $this->setUrl('page='.$this->totalPage); } /*limit 0,5*/ public function limit(){ $offset = ($this->page-1)*$this->number; return $offset.','.$this->number; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。