class Page { private $total; //總記錄 private $pagesize; //每頁顯示多少條 private $limit; //limit private $page; //目前頁碼 private $pagenum; //總頁碼 private $url; //位址 private $bothnum; //兩邊保持數字分頁的量 //建構方法初始化 public function __construct($_total, $_pagesize) { $this->total = $_total ? $_total : 1; $this->total = $_total ? $_total : 1; pagesize = $_pagesize; $this->pagenum = ceil($this->total / $this->pagesize); $this->page = $this->setPage(); $this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize"; $this->url = $this->setUrl (); $this->bothnum = 2; } //攔截器 private function __get($_key) { return $this->$_key; } //取得目前頁碼 private function setPage() { if (!empty($_GET['page'])) { if ($_GET[ 'page'] > 0) { if ($_GET['page'] > $this->pagenum) { return $this->pagenum; } else { return $_GET ['page']; } } else { return 1; } } else { return 1; } } //取得位址 private function setUrl() { $_url = $_SERVER["REQUEST_URI"]; $_par = parse_url($_url); if (Yisset($par_url); ['query'])) { parse_str($_par['query'],$_query); unset($_query['page']); $_url = $_par['path '].'?'.http_build_query($_query); } return $_url; } //數字目錄 private function pageList() { for ($i=$ this->bothnum;$i>=1;$i--) { $_page = $this->page-$i; if ($_page $_pagelist . = ' '.$_page.' '; } $_pagelist .= ' '.$this->page.' '; for ($i=1;$ibothnum;$i ) { $_page = $this->page $i; if ($_page > $this->pagenum) break; $_pagelist .= ' '.$_page.' '; } return $_pagelist; } //首頁 private function first() { if ($this->page > $this->bothnum 1) { return ' 1 . ..'; } } //上一頁 private function prev() { if ($this->page == 1) { return '上一頁'; } return ' 上一頁 '; } //下一頁 private function next() { if ($this- >page == $this->pagenum) { return '下一頁'; } return ' 下一頁 '; } //尾頁 private function last() { if ($this->pagenum - $this->page > $this->bothnum) { return ' .. .'.$this->pagenum.' '; } } //分頁資訊 public function showpage() { $_page .= $this->first(); $_page .= $this->pageList(); $_page .= $this->last(); $_page .= $this->prev(); $_page .= $this->next(); return $ _page; } } ?> 複製程式碼 分頁樣式如下圖: 使用說明: $_page = new Page($_total,$_pagesize); //其中$_total 是資料集的總條數,$_pagesize 是每頁顯示的數量. ?> 複製代碼 驗證碼, PHP