1. 程式人生 > 實用技巧 >完美分頁類

完美分頁類

  1 <?php
  2 /**
  3 file: page.class.php
  4 完美分頁類 Page
  5 */
  6 class Page {
  7 private $total; //資料表中總記錄數
  8 private $listRows; //每頁顯示行數
  9 private $limit; //SQL語句使用limit從句,限制獲取記錄個數
 10 private $uri; //自動獲取url的請求地址
 11 private $pageNum; //總頁數
 12 private $page; //當前頁
 13 private $config = array(
 14 'head' => "條記錄",
 15
'prev' => "上一頁", 16 'next' => "下一頁", 17 'first'=> "首頁", 18 'last' => "末頁" 19 ); 20 //在分頁資訊中顯示內容,可以自己通過set()方法設定 21 private $listNum = 10; //預設分頁列表顯示的個數 22 23 /** 24 構造方法,可以設定分頁類的屬性 25 @param int $total 計算分頁的總記錄數 26 @param int $listRows 可選的,設定每頁需要顯示的記錄數,預設為25條 27 @param mixed $query 可選的,為向目標頁面傳遞引數,可以是陣列,也可以是查詢字串格式
28 @param bool $ord 可選的,預設值為true, 頁面從第一頁開始顯示,false則為最後一頁 29 */ 30 public function __construct($total, $listRows=25, $query="", $ord=true){ 31 $this->total = $total; 32 $this->listRows = $listRows; 33 $this->uri = $this->getUri($query); 34 $this->pageNum = ceil($this->total / $this
->listRows); 35 /*以下判斷用來設定當前面*/ 36 if(!empty($_GET["page"])) { 37 $page = $_GET["page"]; 38 }else{ 39 if($ord) 40 $page = 1; 41 else 42 $page = $this->pageNum; 43 } 44 45 if($total > 0) { 46 if(preg_match('/\D/', $page) ){ 47 $this->page = 1; 48 }else{ 49 $this->page = $page; 50 } 51 }else{ 52 $this->page = 0; 53 } 54 55 $this->limit = "LIMIT ".$this->setLimit(); 56 } 57 58 /** 59 用於設定顯示分頁的資訊,可以進行連貫操作 60 @param string $param 是成員屬性陣列config的下標 61 @param string $value 用於設定config下標對應的元素值 62 @return object 返回本物件自己$this, 用於連慣操作 63 */ 64 function set($param, $value){ 65 if(array_key_exists($param, $this->config)){ 66 $this->config[$param] = $value; 67 } 68 return $this; 69 } 70 71 /* 不是直接去呼叫,通過該方法,可以使用在物件外部直接獲取私有成員屬性limit和page的值 */ 72 function __get($args){ 73 if($args == "limit" || $args == "page") 74 return $this->$args; 75 else 76 return null; 77 } 78 79 /** 80 按指定的格式輸出分頁 81 @param int 0-7的數字分別作為引數,用於自定義輸出分頁結構和調整結構的順序,預設輸出全部結構 82 @return string 分頁資訊內容 83 */ 84 function fpage(){ 85 $arr = func_get_args(); 86 87 $html[0] = "<span class='p1'>&nbsp;共<b> {$this->total} </b>{$this->config["head"]}&nbsp;</span>"; 88 $html[1] = "&nbsp;本頁 <b>".$this->disnum()."</b> 條&nbsp;"; 89 $html[2] = "&nbsp;本頁從 <b>{$this->start()}-{$this->end()}</b> 條&nbsp;"; 90 $html[3] = "&nbsp;<b>{$this->page}/{$this->pageNum}</b>頁&nbsp;"; 91 $html[4] = $this->firstprev(); 92 $html[5] = $this->pageList(); 93 $html[6] = $this->nextlast(); 94 $html[7] = $this->goPage(); 95 96 $fpage = '<div style="font:12px \'\5B8B\4F53\',san-serif;">'; 97 if(count($arr) < 1) 98 $arr = array(0, 1,2,3,4,5,6,7); 99 100 for($i = 0; $i < count($arr); $i++) 101 $fpage .= $html[$arr[$i]]; 102 103 $fpage .= '</div>'; 104 return $fpage; 105 } 106 107 /* 在物件內部使用的私有方法,*/ 108 private function setLimit(){ 109 if($this->page > 0) 110 return ($this->page-1)*$this->listRows.", {$this->listRows}"; 111 else 112 return 0; 113 } 114 115 /* 在物件內部使用的私有方法,用於自動獲取訪問的當前URL */ 116 private function getUri($query){ 117 $request_uri = $_SERVER["REQUEST_URI"]; 118 $url = strstr($request_uri,'?') ? $request_uri : $request_uri.'?'; 119 120 if(is_array($query)) 121 $url .= http_build_query($query); 122 else if($query != "") 123 $url .= "&".trim($query, "?&"); 124 125 $arr = parse_url($url); 126 127 if(isset($arr["query"])){ 128 parse_str($arr["query"], $arrs); 129 unset($arrs["page"]); 130 $url = $arr["path"].'?'.http_build_query($arrs); 131 } 132 133 if(strstr($url, '?')) { 134 if(substr($url, -1)!='?') 135 $url = $url.'&'; 136 }else{ 137 $url = $url.'?'; 138 } 139 140 return $url; 141 } 142 143 /* 在物件內部使用的私有方法,用於獲取當前頁開始的記錄數 */ 144 private function start(){ 145 if($this->total == 0) 146 return 0; 147 else 148 return ($this->page-1) * $this->listRows+1; 149 } 150 151 /* 在物件內部使用的私有方法,用於獲取當前頁結束的記錄數 */ 152 private function end(){ 153 return min($this->page * $this->listRows, $this->total); 154 } 155 156 /* 在物件內部使用的私有方法,用於獲取上一頁和首頁的操作資訊 */ 157 private function firstprev(){ 158 if($this->page > 1) { 159 $str = "&nbsp;<a href='{$this->uri}page=1'>{$this->config["first"]}</a>&nbsp;"; 160 $str .= "<a href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a>&nbsp;"; 161 return $str; 162 } 163 164 } 165 166 /* 在物件內部使用的私有方法,用於獲取頁數列表資訊 */ 167 private function pageList(){ 168 $linkPage = "&nbsp;<b>"; 169 170 $inum = floor($this->listNum/2); 171 /*當前頁前面的列表 */ 172 for($i = $inum; $i >= 1; $i--){ 173 $page = $this->page-$i; 174 175 if($page >= 1) 176 $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;"; 177 } 178 /*當前頁的資訊 */ 179 if($this->pageNum > 1) 180 $linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{$this->page}</span>&nbsp;"; 181 182 /*當前頁後面的列表 */ 183 for($i=1; $i <= $inum; $i++){ 184 $page = $this->page+$i; 185 if($page <= $this->pageNum) 186 $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;"; 187 else 188 break; 189 } 190 $linkPage .= '</b>'; 191 return $linkPage; 192 } 193 194 /* 在物件內部使用的私有方法,獲取下一頁和尾頁的操作資訊 */ 195 private function nextlast(){ 196 if($this->page != $this->pageNum) { 197 $str = "&nbsp;<a href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a>&nbsp;"; 198 $str .= "&nbsp;<a href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a>&nbsp;"; 199 return $str; 200 } 201 } 202 203 /* 在物件內部使用的私有方法,用於顯示和處理表單跳轉頁面 */ 204 private function goPage(){ 205 if($this->pageNum > 1) { 206 return '&nbsp;<input style="width:20px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'page=\'+page+\'\'}" value="'.$this->page.'"><input style="cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'page=\'+page+\'\'">&nbsp;'; 207 } 208 } 209 210 /* 在物件內部使用的私有方法,用於獲取本頁顯示的記錄條數 */ 211 private function disnum(){ 212 if($this->total > 0){ 213 return $this->end()-$this->start()+1; 214 }else{ 215 return 0; 216 } 217 } 218 }