- class dividepage{//分頁(yè)類
- private $total;//要顯示的總記錄數(shù)
- private $url;//請(qǐng)求的url地址
- private $displaypg;//每頁(yè)顯示的記錄數(shù),默認(rèn)為每頁(yè)顯示10條記錄
- private $page;//當(dāng)前頁(yè)碼
- private $lastpg;//總頁(yè)數(shù),即最后一頁(yè)的頁(yè)碼
- private $prepg;//前一頁(yè)
- private $nextpg;//后一頁(yè)
- private $firstcount;//記錄條數(shù)開始的序號(hào)從0開始
- private $startd;//記錄條數(shù)開始的記錄號(hào).
- private $stopd;//記錄條數(shù)結(jié)束的記錄號(hào).
- //構(gòu)造函數(shù)
- public function __construct($url, $total, $displaypg){
- $this->url = $url;//請(qǐng)求的url
- $this->total = $total;//總記錄數(shù)
- //if($displaypg == '')
- $this->displaypg = $displaypg;//每頁(yè)顯示的記錄數(shù)
- $this->initdividepage();//初始化分頁(yè)類
- //echo ','.$this->displaypg;
- }
- //初始化分頁(yè)類
- private function initdividepage(){
- //分析url
- $parse_url = parse_url($this->url);//將url解釋為有固定鍵值對(duì)的數(shù)組
- $url_query = $parse_url['query'];//取出url中的查詢字符串
- if($url_query){//如果有查詢字符串,則刪除查詢字串中當(dāng)前頁(yè)的查詢字段如:&page=$page或page=$page
- ereg('(^|&)page=([0-9]*)', $url_query, $k);
- $this->page = $k[2];//取得當(dāng)前頁(yè)的值
- $url_query = ereg_replace("(^|&)page=$this->page", '', $url_query);//刪除查詢字串中當(dāng)前頁(yè)的查詢字段如:&page=$page或page=$page
- $this->url = str_replace($parse_url['query'], $url_query, $this->url);//保留其他的查詢字串,
- $this->page = $this->page ? $this->page : 1;//w如果查詢字符串中沒(méi)有當(dāng)前頁(yè)的值就設(shè)當(dāng)前頁(yè)為1
- if($url_query){//如果有其他查詢字符串,則以&page=$page形式添加翻頁(yè)查詢字串
- $this->url .= '&page';
- }else{//如果沒(méi)有其他查詢字串,則以page=$page形式添加翻頁(yè)查詢字串
- $this->url .= 'page';
- }
- }else{//如果沒(méi)有查詢字串,則在url后添加?page=$page形式的翻頁(yè)查詢字串
- $this->page = 1;
- $this->url .= '?page';
- }
- $this->lastpg = ceil($this->total / $this->displaypg);//計(jì)算總頁(yè)數(shù),即最后一頁(yè)的頁(yè)碼
- $this->page = min($this->lastpg, $this->page);//如果當(dāng)前頁(yè)大于總頁(yè)數(shù),則當(dāng)前頁(yè)為最后一頁(yè)的頁(yè)碼
- $this->prepg = $this->page - 1;//上一頁(yè)為當(dāng)前頁(yè)減一www.111cn.net
- $this->nextpg = $this->page + 1;//(($this->page == $this->lastpg) ? $this->lastpg : ($this->page + 1));//下一頁(yè)為當(dāng)前頁(yè)加一,如果當(dāng)前頁(yè)為最后一頁(yè),則下一頁(yè)為0
- $this->firstcount = ($this->page - 1) * $this->displaypg;//計(jì)算當(dāng)前頁(yè),記錄條數(shù)開始的記錄號(hào),從0開始.
- $this->startd = $this->total ? ($this->firstcount + 1) : 0;//記錄開始號(hào)從1開始
- $this->stopd = min($this->firstcount + $this->displaypg, $this->total);//記錄結(jié)束號(hào)
- //echo $this->displaypg;
- //echo $this->nextpg.'+=+='.$this->lastpg;
- }
- public function getpageinfo(){//取得當(dāng)前頁(yè)面的基本信息,如:顯示第 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(){//取得通常的分頁(yè)導(dǎo)航,如:首頁(yè) 上一頁(yè) 下一頁(yè) 尾頁(yè)
- $commonnav = '';
- if($this->lastpg == 1){//如果只有一頁(yè),則返回翻頁(yè)導(dǎo)航,退出,不顯示下一頁(yè),上一頁(yè)等。。。
- return $commonnav;
- break;
- }
- $commonnav = '<a href="'.$this->url.'=1" class="compagestyle">首頁(yè)</a>';//設(shè)置首頁(yè)導(dǎo)航,page=1
- if($this->prepg){
- $commonnav .= '<a href="'.$this->url.'='.$this->prepg.'" class="compagestyle">上一頁(yè)</a>';
- }else{
- $commonnav .= '<a class="fcompagestyle">上一頁(yè)</a>';
- }
- if($this->nextpg <= $this->lastpg){
- $commonnav .= '<a href="'.$this->url.'='.$this->nextpg.'" class="compagestyle">下一頁(yè)</a>';
- }else{
- $commonnav .= '<a class="fcompagestyle">下一頁(yè)</a>';
- }
- $commonnav .= '<a href="'.$this->url.'='.$this->lastpg.'" class="compagestyle">尾頁(yè)</a>';//顯示尾頁(yè)鏈接
- return $commonnav;
- }
- //取得跳轉(zhuǎn)分頁(yè)導(dǎo)航,如:第n頁(yè)
- 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){//把當(dāng)前頁(yè)的頁(yè)碼作為默認(rèn)選項(xiàng)
- $jumpnav .= '<option value="'.$i.'" selected>'.$i.'</option>'." ";
- }else{
- $jumpnav .= '<option value="'.$i.'">'.$i.'</option>'." ";
- }
- }
- $jumpnav .= '</select>頁(yè),共<span class="numstyle">'.$this->lastpg.'</span>頁(yè)</span>';
- return $jumpnav;
- }
- //取得所有的分頁(yè)導(dǎo)航
- public function getallpagenav(){
- $temp = $this->getpageinfo().$this->getcommonpagenav().$this->getjumppagenav();//開源代碼Vevb.com
- return $temp;
- }
- //取得當(dāng)前頁(yè)需顯示的記錄,在數(shù)據(jù)庫(kù)教程中的限定范圍,如0-9
- public function getlimitstr(){
- //echo $this->page;
- //echo $this->firstcount;
- //echo $this->dispalypg;
- $temp = $this->firstcount.','.$this->displaypg;
- //echo $temp;
- return $temp;
- }
- }
使用實(shí)例,代碼如下:
- $result=mysql_query("select * from tb_pagetest");//從數(shù)據(jù)庫(kù)中查詢所需顯示的數(shù)據(jù)
- $total=mysql_num_rows($result);//查詢到的數(shù)據(jù)的總條數(shù)
- $pagesize = 5;//每頁(yè)顯示的記錄條數(shù)
- $url = $_server['request_uri'];//請(qǐng)求的uri
- $dividepageclass = new dividepage($url, $total, $pagesize); //創(chuàng)建分頁(yè)類,(類能自動(dòng)初始化)
- $limitstr = $dividepageclass->getlimitstr();//取得當(dāng)前頁(yè)要顯示的記錄開始序號(hào)和每頁(yè)顯示條數(shù),如:0, 5(顯示從0開始的5條記錄)
- echo $dividepageclass->getallpagenav();//顯示所有分頁(yè)導(dǎo)航條,
- 如:顯示第11-13條記錄,共13條記錄。首頁(yè) 上一頁(yè) 下一頁(yè) 尾頁(yè) 到*第 1 頁(yè),共 3 頁(yè)
- $sql = 'select * from tb_pagetest limit '.$limitstr;
- $result=mysql_query($sql);//從數(shù)據(jù)庫(kù)中取得當(dāng)前頁(yè)要顯示的記錄集,然后顯示就ok
- 如:
- while($row=mysql_fetch_array($result))
- echo "<hr><b>".$row[title]." | ".$row[author];
新聞熱點(diǎn)
疑難解答