国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

php通用分頁類代碼帶實例教程

2024-04-25 20:36:36
字體:
來源:轉載
供稿:網友

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>';
} //開源代碼www.bcty365.com

$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];

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 图们市| 济阳县| 江源县| 康平县| 新民市| 肥乡县| 集安市| 和田县| 东兴市| 柘城县| 黄陵县| 抚松县| 辽宁省| 九龙坡区| 南京市| 昆明市| 武鸣县| 临颍县| 南投市| 英德市| 常德市| 米脂县| 九龙县| 靖江市| 临武县| 屯留县| 阳春市| 新疆| 台山市| 临潭县| 延吉市| 章丘市| 洛阳市| 九龙坡区| 开远市| 栖霞市| 兰坪| 榕江县| 乐山市| 大宁县| 焦作市|