您好,登錄后才能下訂單哦!
<?php $page = new Page(53,10); $p = $page->rendor(); echo '<pre>'; var_dump($p); echo'</pre>'; /** * 分頁類 */ class Page { protected $url; //URL protected $pageCount; //總頁數(shù) protected $total; //總條數(shù) protected $num; //每頁顯示數(shù) protected $page; //當(dāng)前頁 //初始化成員屬性 public function __construct($total,$num = 5) { //總條數(shù) $this->total = ($total > 0 )? (int) $total : 1; //每頁顯示條數(shù) $this->num = $num; //總頁數(shù) $this->pageCount = $this->getPageCount(); //當(dāng)前頁 $this->page = $this->getCurrentPage(); //URL $this->url = $this->getUrl(); } //一次性返回所有的分頁信息 public function rendor() { return[ 'first' => $this->first(), 'next' => $this->next(), 'prev' => $this->prev(), 'end' => $this->end(), ]; } //limit方法,在未來分頁數(shù)據(jù)查詢的時(shí)候,直接返回對應(yīng)的limit0,5 這樣的字符串 public function limit() { $offset = ($this->page - 1) * $this->num; $str = $offset.','.$this->num; return $str; } //首頁,設(shè)置page = 1 protected function first() { return $this->setQueryString('page=1'); } //上一頁 protected function prev() { $page = ($this->page <= 1) ? 1 : ($this->page - 1); return $this->setQueryString('page='.$page); } //下一頁 protected function next() { $page = ($this->page >= $this->pageCount) ? $this->pageCount : ($this->page + 1); return $this->setQueryString('page='.$page); } //首頁 protected function end() { return $this->setQueryString('page='.$this->pageCount); } //一種情況有? 一種情況沒有? protected function setQueryString($page) { //查找url中是否有問號 if (stripos($this->url, '?')) { return $this->url.'&'.$page; } else { //沒有問號就拼接 return $this->url.'?'.$page; } } //處理URL protected function getUrl() { //獲取用戶的uri $path = $_SERVER['REQUEST_URI']; //解析uri $par = parse_url($path); //判斷用戶是否設(shè)置過query if (isset($par['query'])) { parse_str($par['query'],$query); //檢查query 里面時(shí)候有page,如果有的話就干掉page unset($query['page']); $path = $par['path'] . '?'.http_build_query($query); } $path = rtrim($path,'?'); //協(xié)議:主機(jī):端口:文件和請求 //判斷是否定義過端口,并且端口是否為443,如果為443則是https協(xié)議,否則就是http協(xié)議 $protocal = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ? 'https//' : 'http://'; if (80 == $_SERVER['SERVER_PORT'] || 443 == $_SERVER['SERVER_PORT']) { $url = $protocal.$_SERVER['SERVER_NAME'].$path; }else{ $url = $protocal.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$path; } //全新的url return $url; } //處理當(dāng)前頁P(yáng)age protected function getCurrentPage() { //如果有page,就返回轉(zhuǎn)換為int的page if (isset($_GET['page'])) { //得到頁碼 $page = (int) $_GET['page']; //當(dāng)前頁碼不能給大于總頁碼 if ($page > $this->pageCount) { $page = $this->pageCount; } if ($page < 1) { $page = 1; } }else { $page = 1; } return $page; } //處理總頁數(shù) protected function getPageCount() { //進(jìn)一法取整 return ceil($this->total / $this->num); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。