Class PAGE { //類開始 /**************************************************** **** * $total 記錄總數 * $pageNum 每頁顯示的條數 * $url = '' 連結 * $page->StartPage(顯示分類統計,字元分類/數字分頁,跳轉); * $page->StartPage(true/false, true/false, true/false); * site http://bbs.it-home.org ************************************************** *******/ private $total; //記錄總數 private $pageNum; //每頁顯示數字 private $page; //目前頁數 private $pages; //總的頁數 private $url; //頁面url private $Aque; //URL參數 /* 建構子*/ public function PAGE($total, $pageNum, $url='?' ){ $this->total = $total; //總記錄數. $this->pageNum = $pageNum; //每頁顯示數. $this->url = $this ->StrSift($url); //判斷$url的值是否合法. $this->Aque = $_GET; //頁面原來所傳遞參數. $this->page = $this- >StrSift($_GET['page']); //目前頁面GET(全域變數)方式參數,目前頁碼. $this->page = is_numeric($this->page) ? $this->page : 1; //當頁碼不為數字時,則設為1. $this->pages = ceil($total/$pageNum); //總頁數. if($this ->pagepage = 1; //當頁碼小於1時,則設為1. //if($this->page>$this->pages) $this ->page = $this->pages; //當頁碼大於最大頁碼時,則把其設為最大頁碼. } /****************** * 分頁方式 *******************/ function StartPage($str , $view=true, $jump=true){ if($view==true) $PageStr .= $this->GetCount(); //分頁統計資訊 if($str==' str') $PageStr .= $this->GetPageStr(); //選擇字元分頁形式 else $PageStr .= $this->GetPageNum(); //選擇數字分頁形式 if($jump ==true) $PageStr .= $this->JumpSelect(); //跳轉 return $PageStr; } /********************************************** * 顯示統計資訊. 格式:共5筆記錄頁:2/3 ********************************** *************/ function GetCount(){ $CountStr = "共". $this->total ."筆錄 頁:".$this->page."/".$this- >pages." "; return $CountStr; } /*********************************************** * 分頁格式形一:第一頁上一頁下一頁末頁 ******************************** ****************/ function GetPageStr(){ $url = $ this->url; //取得URL //對URL參數進行處理:數組的鍵是URL變量,數組的值是URL變量的值. foreach($this->Aque as $key = > $val){ switch($key){ case "page": $Next = $val + 1; $Prev = $val - 1; break; default: $Sque .= "&$key=".$this->StrSift($val); } } if($Next==0) $Next= 2; //首頁上一頁 switch($this->page){ case $this->page $pagestr .= "首頁 "; $pagestr .= "上一頁 "; break; default: $pagestr .= "首頁 a> "; $pagestr .= "上一頁 "; } //下一頁末頁 switch($this->page){ case $this->page>=$this->pages: $pagestr .= "下一頁 "; $pagestr .= "末頁 "; break; default: $pagestr .= "下一頁 "; $pagestr .= "末頁 "; } //回傳分頁字串. return $pagestr; } /**************************************************** ******* * 分頁格式形如:共4307筆記錄頁:1/72 1 2 3 4 5 6 7 8 9 10 ************ ************************************************/ function GetPageNum(){ $url = $this->url; //對URL參數處理:陣列的鍵是URL變量,數組的值是URL變量的值. foreach($this->Aque as $key => $val){ switch($key){ case $key! ="page": $Sque .= "&$key=".$this->StrSift($val); } } switch($this->pages){ //總頁數大於12頁: case $this->pages>12: //分頁數字前: switch($this->page){ case $this->page>1: $pagestr .= " "; $pagestr .= " "; break; default: $pagestr .= " $pagestr .= " } //分頁數字:1 2 3 4 5 6 當前頁碼左邊6個分頁連結,右邊6個分頁連結. for($i=$this->page-6; $ipage+6; $i++){ if($i>$this->pages) break; if($i==$this->page) $pagestr .= $i." "; elseif($i>=1) $pagestr .= "$i "; } //分頁數字後: > >> switch($this->page){ case $this->pagepages: $pagestr .= "> > "; $pagestr .= "> "; break ; default: $pagestr .= "> "; $pagestr .= ">> "; } break; default: //總頁數小於12頁: for($i=1; $ipages; $i++){ switch($i){ case $i==$this-> page: $pagestr .= $i." "; break; default: $pagestr .= "$i "; } } } //回傳分頁字串. return $pagestr; } /************************ * 定義跳轉頁. BEGIN *************** **********/ function JumpSelect(){ $url = $this->url; //對URL參數進行處理:數組的鍵是URL變量,數組的值是URL變數的值. foreach($this->Aque as $key => $val){ if($key != "page") $Sque .= "&$key=".$this ->StrSift($val); } $SelectStr = "n $SelectStr .= "onChange=" window.location='".$url."?page='+this.value+'".$Sque."'">n"; for($i=1; $i pages; $i++){ if($i == $this->page) $selected = " selected"; else $selected = ""; $SelectStr .= "$in"; } $SelectStr .= "n"; //傳回分頁字串. return $SelectStr; } /********************************** * 過濾特殊字元. ******* ****************************/ private function StrSift($str){ $str = str_replace(""""," ",$str); $str = str_replace("'","",$str); $str = str_replace("[url=file://%22,%22%22,$ str/]\","",$str[/url]); $str = str_replace("/","",$str); $str = str_replace(":","" ,$str); $str = str_replace("?","",$str); //移除會出現"??". $str = str_replace(">","",$ str); $str = str_replace(" $str = str_replace("%","",$str); $str = str_replace(" *","",$str); $str = str_replace("&","",$str); $str = str_replace(".","",$str); return $str; } //類結束 } ?> 複製程式碼 有興趣的朋友,也可以參考下如下的文章: 一個不錯的php分頁類別的程式碼 一個實用的php分頁類 分頁樣式表: <li>/*下拉選單或文字輸入框樣式*/<li>#JumpSelect{<li>background:#159bd0;<li>border:1px solid #ffff66; <li>color:#ffff66;<li>width:37px;<li>font-size:11px;<li>padding:0 0 000; <li>margin:0 0 0 0;<li>SCROLLBAR-FACE-COLOR: #d0dff7; <li>SCROLLBAR-HIGHLIGHT-COLOR: #d0dff7; <li>SCRO -3DLIGHT-COLOR: #FFFFFF; <li>SCROLLLLBAR-ARROW-COLOR: #ffffff; <li>SCROLLBAR-TRACK-COLOR: #ffffff; <li>SCROLLBAR-DARKSHADOW-COLOR: #ffffff; <li>SCROLLBAR-DARKSHADOW-COLOR: #ffffff; <li>SCROLLBAR-DARKSHADOW-COLOR: #d0d7; : #eded;<li> scrollbar-arrow-color: #000;<li> scrollbar-highlight-color: #fafafa;<li> scrollbar-shadow-color: #fafafa;<li> scrollbar-track-color: # ededed;<li> scrollbar-face-color: #fafafa;<li> scrollbar-darkshadow-color: #ededed;<li> scrollbar-3dlight-color: #ededed;<li>}<li> 複製程式碼