php通用分頁類代碼

來源:互聯網
上載者:User

 

 代碼如下 複製代碼

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;
}

}

 

/*
使用執行個體:

 代碼如下 複製代碼
*$result=mysql教程_query("select * from tb_pagetest");//從資料庫中查詢所需顯示的資料
*$total=mysql_num_rows($result);//查詢到的資料的總條數
*$pagesize = 5;//每頁顯示的記錄條數
*$url = $_server['request_uri'];//請求的uri
*
*$dividepageclass = new dividepage($url, $total, $pagesize); //建立分頁類,(類能自動初始化)
*$limitstr = $dividepageclass->getlimitstr();//取得當前頁要顯示的記錄開始序號和每頁顯示條數,如:0, 5(顯示從0開始的5條記錄)
*echo $dividepageclass->getallpagenav();//顯示所有分頁導航條,
*如:顯示第11-13條記錄,共13條記錄。首頁 上一頁 下一頁 尾頁 到*第 1 頁,共 3 頁
*$sql = 'select * from tb_pagetest limit '.$limitstr;
*$result=mysql_query($sql);//從資料庫中取得當前頁要顯示的記錄集,然後顯示就ok
*如:
*while($row=mysql_fetch_array($result))
*echo "<hr><b>".$row[title]." | ".$row[author];

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.