php通用分頁類代碼_PHP教程

來源:互聯網
上載者:User
php通用分頁類代碼這是一款使用方法的php分頁代碼,我們把它總結了各種分頁代碼的特別,寫出一款php通用分頁類使用方法簡單,易懂哦。

php教程通用分頁類代碼
這是一款使用方法的php分頁代碼,我們把它總結了各種分頁代碼的特別,寫出一款php通用分頁類使用方法簡單,易懂哦。
*/

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.bKjia.c0m
$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 '顯示第'.$this->startd.'-'.$this->stopd.'條記錄,共'.$this->total.'條記錄。';
}

public function getcommonpagenav(){//取得通常的分頁導航,如:首頁 上一頁 下一頁 尾頁
$commonnav = '';
if($this->lastpg == 1){//如果只有一頁,則返回翻頁導航,退出,不顯示下一頁,上一頁等。。。
return $commonnav;
break;
}
$commonnav = 'url.'=1" class="compagestyle">首頁';//設定首頁導航,page=1
if($this->prepg){
$commonnav .= 'url.'='.$this->prepg.'" class="compagestyle">上一頁';
}else{
$commonnav .= '上一頁';
}
if($this->nextpg <= $this->lastpg){
$commonnav .= 'url.'='.$this->nextpg.'" class="compagestyle">下一頁';
}else{
$commonnav .= '下一頁';
}
$commonnav .= 'url.'='.$this->lastpg.'" class="compagestyle">尾頁';//顯示尾頁連結
return $commonnav;
}

//取得跳轉分頁導航,如:第n頁
public function getjumppagenav(){
// $jumpnav = '到第url.'="+this.value'>'."n";
for($i = 1; $i <= $this->lastpg; $i++){
if($i == $this->page){//把當前頁的頁碼作為預設選項
$jumpnav .= ''.$i.''."n";
}else{
$jumpnav .= ''.$i.''."n";
}
}
$jumpnav .= '頁,共'.$this->lastpg.'頁';
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 " ".$row[title]." | ".$row[author];

http://www.bkjia.com/PHPjc/630833.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630833.htmlTechArticlephp通用分頁類代碼這是一款使用方法的php分頁代碼,我們把它總結了各種分頁代碼的特別,寫出一款php通用分頁類使用方法簡單,易懂哦。...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.