class dividepage{//分頁類 private $total;//要顯示的總記錄數 private $url;//請求的url地址 private $displaypg;//每頁顯示的記錄數,預設為每頁顯示10條記錄 private $page;//當前頁碼 private $lastpg;//總頁數,即最後一頁的頁碼 private $prepg;//前一頁 private $nextpg;//後一頁 private $firstcount;//記錄條數開始的序號從0開始 private $startd;//記錄條數開始的記錄號. private $stopd;//記錄條數結束的記錄號. //建構函式 public function __construct($url, $total, $displaypg){ $this->url = $url;//請求的url $this->total = $total;//總記錄數 //if($displaypg == '') $this->displaypg = $displaypg;//每頁顯示的記錄數 $this->initdividepage();//初始化分頁類 //echo ','.$this->displaypg; } //初始化分頁類 private function initdividepage(){ //分析url $parse_url = parse_url($this->url);//將url解釋為有固定索引值對的數組 $url_query = $parse_url['query'];//取出url中的查詢字串 if($url_query){//如果有查詢字串,則刪除查詢字串中當前頁的查詢欄位如:&page=$page或page=$page ereg('(^|&)page=([0-9]*)', $url_query, $k); $this->page = $k[2];//取得當前頁的值 $url_query = ereg_replace("(^|&)page=$this->page", '', $url_query);//刪除查詢字串中當前頁的查詢欄位如:&page=$page或page=$page $this->url = str_replace($parse_url['query'], $url_query, $this->url);//保留其他的查詢字串, $this->page = $this->page ? $this->page : 1;//w如果查詢字串中沒有當前頁的值就設當前頁為1 if($url_query){//如果有其他查詢字串,則以&page=$page形式添加翻頁查詢字串 $this->url .= '&page'; }else{//如果沒有其他查詢字串,則以page=$page形式添加翻頁查詢字串 $this->url .= 'page'; } }else{//如果沒有查詢字串,則在url後添加?page=$page形式的翻頁查詢字串 $this->page = 1; $this->url .= '?page'; } $this->lastpg = ceil($this->total / $this->displaypg);//計算總頁數,即最後一頁的頁碼 $this->page = min($this->lastpg, $this->page);//如果當前頁大於總頁數,則當前頁為最後一頁的頁碼 $this->prepg = $this->page - 1;//上一頁為當前頁減一www.111cn.net $this->nextpg = $this->page + 1;//(($this->page == $this->lastpg) ? $this->lastpg : ($this->page + 1));//下一頁為當前頁加一,如果當前頁為最後一頁,則下一頁為0 $this->firstcount = ($this->page - 1) * $this->displaypg;//計算當前頁,記錄條數開始的記錄號,從0開始. $this->startd = $this->total ? ($this->firstcount + 1) : 0;//記錄開始號從1開始 $this->stopd = min($this->firstcount + $this->displaypg, $this->total);//記錄結束號 //echo $this->displaypg; //echo $this->nextpg.'+=+='.$this->lastpg; } public function getpageinfo(){//取得當前頁面的基本資料,如:顯示第 1-10 條記錄,共 23 條記錄。 return '<span class="pageinfostyle">顯示第<span class="numstyle">'.$this->startd.'-'.$this->stopd.'</span>條記錄,共<span class="numstyle">'.$this->total.'</span>條記錄。</span>'; } public function getcommonpagenav(){//取得通常的分頁導航,如:首頁 上一頁 下一頁 尾頁 $commonnav = ''; if($this->lastpg == 1){//如果只有一頁,則返回翻頁導航,退出,不顯示下一頁,上一頁等。。。 return $commonnav; break; } $commonnav = '<a href="'.$this->url.'=1" class="compagestyle">首頁</a>';//設定首頁導航,page=1 if($this->prepg){ $commonnav .= '<a href="'.$this->url.'='.$this->prepg.'" class="compagestyle">上一頁</a>'; }else{ $commonnav .= '<a class="fcompagestyle">上一頁</a>'; } if($this->nextpg <= $this->lastpg){ $commonnav .= '<a href="'.$this->url.'='.$this->nextpg.'" class="compagestyle">下一頁</a>'; }else{ $commonnav .= '<a class="fcompagestyle">下一頁</a>'; } $commonnav .= '<a href="'.$this->url.'='.$this->lastpg.'" class="compagestyle">尾頁</a>';//顯示尾頁連結 return $commonnav; } //取得跳轉分頁導航,如:第n頁 public function getjumppagenav(){ //<select name='topage' size='1' onchange='window.location="/test/page.php?page="+this.value'> $jumpnav = '<span class="pageinfostyle">到第<select name="topage" size="1" class="topage" onchange='window.location="'.$this->url.'="+this.value'>'." "; for($i = 1; $i <= $this->lastpg; $i++){ if($i == $this->page){//把當前頁的頁碼作為預設選項 $jumpnav .= '<option value="'.$i.'" selected>'.$i.'</option>'." "; }else{ $jumpnav .= '<option value="'.$i.'">'.$i.'</option>'." "; } } $jumpnav .= '</select>頁,共<span class="numstyle">'.$this->lastpg.'</span>頁</span>'; return $jumpnav; } //取得所有的分頁導航 public function getallpagenav(){ $temp = $this->getpageinfo().$this->getcommonpagenav().$this->getjumppagenav(); return $temp; } //取得當前頁需顯示的記錄,在資料庫教程中的限定範圍,如0-9 public function getlimitstr(){ //echo $this->page; //echo $this->firstcount; //echo $this->dispalypg; $temp = $this->firstcount.','.$this->displaypg; //echo $temp; return $temp; } } |