您好,登錄后才能下訂單哦!
<?php $page = new Page(53,10); $p = $page->rendor(); echo '<pre>'; var_dump($p); echo'</pre>'; /** * 分頁類 */ class Page { protected $url; //URL protected $pageCount; //總頁數 protected $total; //總條數 protected $num; //每頁顯示數 protected $page; //當前頁 //初始化成員屬性 public function __construct($total,$num = 5) { //總條數 $this->total = ($total > 0 )? (int) $total : 1; //每頁顯示條數 $this->num = $num; //總頁數 $this->pageCount = $this->getPageCount(); //當前頁 $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方法,在未來分頁數據查詢的時候,直接返回對應的limit0,5 這樣的字符串 public function limit() { $offset = ($this->page - 1) * $this->num; $str = $offset.','.$this->num; return $str; } //首頁,設置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); //判斷用戶是否設置過query if (isset($par['query'])) { parse_str($par['query'],$query); //檢查query 里面時候有page,如果有的話就干掉page unset($query['page']); $path = $par['path'] . '?'.http_build_query($query); } $path = rtrim($path,'?'); //協議:主機:端口:文件和請求 //判斷是否定義過端口,并且端口是否為443,如果為443則是https協議,否則就是http協議 $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; } //處理當前頁Page protected function getCurrentPage() { //如果有page,就返回轉換為int的page if (isset($_GET['page'])) { //得到頁碼 $page = (int) $_GET['page']; //當前頁碼不能給大于總頁碼 if ($page > $this->pageCount) { $page = $this->pageCount; } if ($page < 1) { $page = 1; } }else { $page = 1; } return $page; } //處理總頁數 protected function getPageCount() { //進一法取整 return ceil($this->total / $this->num); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。