- class Page{
- private $total; //資料表中總記錄數
- private $listRows; //每頁顯示的條數
- private $limit; //SQL語句中使用limit從句,限制擷取記錄數
- private $uri; //自動擷取url的請求地址
- private $pageNum; //總頁數
- private $page; //當前頁
- private $config = array(
- 'head' => '條記錄',
- 'prev' => '上一頁',
- 'next' => '下一頁',
- 'first' => '首頁',
- 'last' => '末頁'
- );//在分頁資訊中顯示內容,可以自己通過set()方法設定
- private $listNum = 5; //預設分頁列表顯示的個數
-
- /**
- * 構造方法,可以設定分頁類的屬性
- * @param int $total計算分頁的總記錄數
- * @param int $listRows 可選的,設定每頁顯示的記錄數,預設25
- * @param mixed $query可選的,為向目標頁面傳遞參數,可以是數組,也可以是查詢字串格式
- * @param bool $ord 預設是true,頁面從第一頁開始顯示,false則從最後一頁開始顯示
- */ bbs.it-home.org
- public function __construct($total, $listRows=25, $query='', $ord=true){
- $this->total = $total;
- $this->listRows = $listRows;
- $this->uri = $this->getUri($query);
- $this->pageNum = ceil($this->total/$this->listRows);
-
- /*以下判斷用來設定當前頁*/
- if(!empty($_GET['page'])){
- $page = $_GET['page'];
- }else{
- if($ord){
- $page = 1;
- }else{
- $page = $this->pageNum;
- }
- }
-
- if($total > 0){
- if(preg_match('/\D/', $page)){
- $this->page = 1;
- }else{
- $this->page = $page;
- }
- }
-
- //$this->limit = 'LIMIT '.$this->setLimit();
- $this->limit = $this->setLimit();
- }
-
- /**
- * 用於設定顯示分頁的資訊,可以進行連貫操作
- * @param string $param 成員屬性數組config的下表
- * @param string $value 用於設定config下標對應的元素值
- * @return object 返回本對象自己$this,用於連貫操作
- */
- function __set($param, $value){
- if(array_key_exists($param, $this->config)){
- $this->config[$param] = $value;
- }
- //return $this;
- }
-
- /*可以使用在對象外部,直接擷取私人成員屬性limit和page的值*/
- function __get($args){
- if($args == 'limit' || $args == 'page'){
- return $this->$args;
- }else{
- return null;
- }
- }
-
- /**
- * 按指定的格式輸出分頁
- * @param int 0-7的數字分別作為參數,用於自訂輸出分頁結構和調整結構的順序,預設輸出全部結構
- * @return string 分頁資訊內容
- */
- function fpage(){
- $arr = func_get_args();
-
- $html[0] = " 共 {$this->total} {$this->config['head']} ";
- $html[1] = " 本頁".$this->disnum()."條 ";
- $html[2] = " 本頁從{$this->start()} - {$this->end()}條 ";
- $html[3] = " {$this->page}/{$this->pageNum}頁 ";
- $html[4] = $this->firstprev();
- $html[5] = $this->pageList();
- $html[6] = $this->nextlast();
- $html[7] = $this->goPage();
-
- $fpage = '';
-
- if(count($arr)<1){
- $arr = array(0,1,2,3,4,5,6,7);
- }
-
- for($i=0; $i$fpage .= $html[$arr[$i]];
- }
-
- $fpage .= '';
- return $fpage;
- }
-
- /*在對象內部使用,用於自動擷取訪問的當前url*/
- private function getUri($query){
- $request_uri = $_SERVER['REQUEST_URI'];
- $url = strstr($request_uri, '?') ? $request_uri : $request_uri.'?';
-
- if(is_array($query)){
- $url .= http_build_query($query);
- }else if($query != ''){
- $url .= '&'.trim($query, '?&');
- }
-
- $arr = parse_url($url);
-
- if(isset($arr['query'])){
- parse_str($arr['query'], $arrs);
- unset($arrs['page']);
- $url = $arr['path'].'?'.http_build_query($arrs);
- }
-
- if(strstr($url, '?')){
- if(substr($url, -1) != '?'){
- $url = $url.'&';
- }
- }else{
- $url .= '?';
- }
- return $url;
- }
-
- /*私人方法,設定limit*/
- private function setLimit(){
- if($this->page > 0){
- return ($this->page - 1)*$this->listRows.", {$this->listRows}";
- }else{
- return 0;
- }
- }
-
- /*在對象內部使用的私人方法,用於擷取當前頁開始的記錄數*/
- private function start(){
- if($this->total == 0){
- return 0;
- }else{
- return ($this->page-1) * $this->listRows + 1;
- }
- }
-
- /*用於擷取當前頁結束的記錄數*/
- private function end(){
- return min($this->page * $this->listRows, $this->total);
- }
-
- /*用於擷取本頁顯示的記錄條數*/
- private function disnum(){
- if($this->total > 0){
- return $this->end() - $this->start() + 1;
- }else{
- return 0;
- }
- }
-
- /*用於擷取上一頁和首頁的操作資訊*/
- private function firstprev(){
- if($this->page > 1){
- $str = " uri}page=1'>{$this->config['first']} ";
- $str .= "uri}page=".($this->page-1)."'>{$this->config['prev']} ";
- return $str;
- }
- }
-
- private function pageList(){
- $linkPage = ' ';
- $pageSub = $this->page%$this->listNum;
- if($pageSub==0 && $this->page>0){
- $pageSub = $this->listNum;
- }
- /*當前頁面前面的列表*/
- for($i=$pageSub-1; $i >= 1; $i--){
- $page = $this->page-$i;
- if($page >= 1){
- $linkPage .= "uri}page={$page}'>{$page} ";
- }
- }
-
- /*當前頁的資訊*/
- if($this->pageNum > 1){
- $linkPage .= "{$this->page} ";
- }
-
- /*當前頁後面的列表*/
- for($i=1; $i<=$this->listNum-$pageSub; $i++){
- $page = $this->page + $i;
- if($page<=$this->pageNum){
- $linkPage .= "uri}page={$page}'>{$page} ";
- }else{
- break;
- }
- }
-
- $linkPage .= '';
- return $linkPage;
- }
-
- /*用於擷取頁數列表資訊*/
- private function pageListBak(){
- $linkPage = ' ';
-
- $inum = floor($this->listNum/2);
- /*當前頁面前面的列表*/
- for($i=$inum; $i >= 1; $i--){
- $page = $this->page-$i;
- if($page >= 1){
- $linkPage .= "uri}page={$page}'>{$page} ";
- }
- }
-
- /*當前頁的資訊*/
- if($this->pageNum > 1){
- $linkPage .= "{$this->page} ";
- }
-
- /*當前頁後面的列表*/
- for($i=1; $i<=$inum; $i++){
- $page = $this->page + $i;
- if($page<=$this->pageNum){
- $linkPage .= "uri}page={$page}'>{$page} ";
- }else{
- break;
- }
- }
-
- $linkPage .= '';
- return $linkPage;
- }
-
- /*擷取下一頁和尾頁的操作資訊*/
- private function nextlast(){
- if($this->page != $this->pageNum){
- $str = " uri}page=".($this->page+1)."'>{$this->config['next']} ";
- $str .= " uri}page={$this->pageNum}'>{$this->config['last']} ";
- return $str;
- }
- }
-
- /*用於顯示和處理表單跳轉頁面*/
- private function goPage(){
- if($this->pageNum > 1){
- return ' ';
- }
- }
- }
複製代碼 |