您好,登錄后才能下訂單哦!
分頁類
<?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> <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 .= ' <a href="'.$this->url.'&page='.$page.'">'.$page.'</a> '; } } //當前頁的設置 if($this->pages>1){ $list .=' '.$this->cpage.' '; } //當前頁之后的設置 for($i=1;$i <=$num;$i++){ $page = $this->cpage+$i; if($page <= $this->pages){ $list .= ' <a href="'.$this->url.'&page='.$page.'">'.$page.'</a> '; }else{ break; } } return $list; } //末頁 private function last(){ if($this->cpage < $this->pages){ $next = $this->cpage+1; return '<a href="'.$this->url.'&page='.$next.'">下一頁</a> <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]=" 共{$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);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。