PHP分頁(yè)顯示的方法分析【附PHP通用分頁(yè)類(lèi)】
本文實(shí)例講述了PHP分頁(yè)顯示的方法。分享給大家供大家參考,具體如下:
<?php
header("content-type:text/html;charset=utf-8");
$currentpage = 1;
if(isset($_GET['page']))
$currentpage = $_GET['page'];
//連接數(shù)據(jù)庫(kù)
$link = mysql_connect("localhost","root","") or die('連接失敗');
mysql_select_db('myschool');
mysql_query('set names utf8');
$sql ="SELECT count(*) as 'count' from student";//查詢記錄的sql語(yǔ)句
$result = mysql_query($sql);
$arr = mysql_fetch_array($result);
$count = $arr['count'];
$pagesize = 3;
$pages = ceil($count/$pagesize);//共多少頁(yè)
$prepage = $currentpage -1;
if($prepage<=0)
$prepage=1;
$nextpage = $currentpage+1;
if($nextpage >= $pages){
$nextpage = $pages;
}
$start =($currentpage-1) * $pagesize;//起始位置
$sql = "SELECT * from student limit $start,$pagesize";
echo $sql;
// $sql = "select * from student";
$result = mysql_query($sql);
?>
<!-- html部分 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1">
<tr>
<td>學(xué)號(hào)</td>
<td>姓名</td>
<td>性別</td>
<td>年齡</td>
</tr>
<?php while($arr=mysql_fetch_array($result)){ ?>
<td><?php echo $arr['number']; ?></td>
<td><?php echo $arr['name']; ?></td>
<td><?php echo $arr['sex']; ?></td>
<td><?php echo $arr['age']; ?></td>
</tr>
<?php } ?>
</table>
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$prepage; ?>" rel="external nofollow" >上一頁(yè)</a> <a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$nextpage; ?>" rel="external nofollow" >下一頁(yè)</a>
</body>
</html>
注:當(dāng)一個(gè)文件中有php和html兩種時(shí),php文件必須有結(jié)束標(biāo)記
附:php通用分頁(yè)類(lèi)與用法:
Page.class.php文件:
<?php
/**
* 分頁(yè)類(lèi)
*
* 調(diào)用方式:
* $p=new Page(總條數(shù),顯示頁(yè)數(shù),當(dāng)前頁(yè)碼,每頁(yè)顯示條數(shù),[鏈接]);
* print_r($p->getPages()); //生成一個(gè)頁(yè)碼數(shù)組(鍵為頁(yè)碼,值為鏈接)
* echo $p->showPages(1); //生成一個(gè)頁(yè)碼樣式(可添加自定義樣式)
*
*/
/*
總條數(shù),需要顯示的頁(yè)數(shù),當(dāng)前頁(yè),每頁(yè)顯示的條數(shù),連接
生成一個(gè)一維數(shù)組,鍵為頁(yè)碼 值為連接
返回一個(gè)生成好樣式的頁(yè)碼(并且可以根據(jù)自己需要添加樣式)
默認(rèn)樣式 共45條記錄,每頁(yè)顯示10條,當(dāng)前第1/4頁(yè) [首頁(yè)] [上頁(yè)] [1] [2] [3] .. [下頁(yè)] [尾頁(yè)]
*/
class Page{
protected $count; //總條數(shù)
protected $showPages; //需要顯示的頁(yè)數(shù)
protected $countPages; //總頁(yè)數(shù)
protected $currPage; //當(dāng)前頁(yè)
protected $subPages; //每頁(yè)顯示條數(shù)
protected $href; //連接
protected $page_arr=array(); //保存生成的頁(yè)碼 鍵頁(yè)碼 值為連接
/**
* __construct 構(gòu)造函數(shù)(獲取分頁(yè)所需參數(shù))
* @param int $count 總條數(shù)
* @param int $showPages 顯示頁(yè)數(shù)
* @param int $currPage 當(dāng)前頁(yè)數(shù)
* @param int $subPages 每頁(yè)顯示數(shù)量
* @param string $href 連接(不設(shè)置則獲取當(dāng)前URL)
*/
public function __construct($count,$showPages,$currPage,$subPages,$href=''){
$this->count=$count;
$this->showPages=$showPages;
$this->currPage=$currPage;
$this->subPages=$subPages;
//如果鏈接沒(méi)有設(shè)置則獲取當(dāng)前連接
if(empty($href)){
$this->href=htmlentities($_SERVER['PHP_SELF']);
}else{
$this->href=$href;
}
$this->construct_Pages();
}
/**
* getPages 返回頁(yè)碼數(shù)組
* @return array 一維數(shù)組 鍵為頁(yè)碼 值為鏈接
*/
public function getPages(){
return $this->page_arr;
}
/**
* showPages 返回生成好的頁(yè)碼
* @param int $style 樣式
* @return string 生成好的頁(yè)碼
*/
public function showPages($style=1){
$func='pageStyle'.$style;
return $this->$func();
}
/**
* pageStyle1 分頁(yè)樣式(可參照這個(gè)添加自定義樣式 例如pageStyle2())
* 樣式 共45條記錄,每頁(yè)顯示10條,當(dāng)前第1/4頁(yè) [首頁(yè)] [上頁(yè)] [1] [2] [3] .. [下頁(yè)] [尾頁(yè)]
* @return string
*/
protected function pageStyle1(){
/* 構(gòu)造普通模式的分頁(yè)
共4523條記錄,每頁(yè)顯示10條,當(dāng)前第1/453頁(yè) [首頁(yè)] [上頁(yè)] [1] [2] [3] .. [下頁(yè)] [尾頁(yè)]
*/
$pageStr='共'.$this->count.'條記錄,每頁(yè)顯示'.$this->subPages.'條';
$pageStr.='當(dāng)前第'.$this->currPage.'/'.$this->countPages.'頁(yè) ';
$_GET['page'] = 1;
$pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a>] </span>';
//如果當(dāng)前頁(yè)不是第一頁(yè)就顯示上頁(yè)
if($this->currPage>1){
$_GET['page'] = $this->currPage-1;
$pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上頁(yè)</a>] </span>';
}
foreach ($this->page_arr as $k => $v) {
$_GET['page'] = $k;
$pageStr.='<span>[<a href="'.$v.'" rel="external nofollow" >'.$k.'</a>] </span>';
}
//如果當(dāng)前頁(yè)小于總頁(yè)數(shù)就顯示下一頁(yè)
if($this->currPage<$this->countPages){
$_GET['page'] = $this->currPage+1;
$pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下頁(yè)</a>] </span>';
}
$_GET['page'] = $this->countPages;
$pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a>] </span>';
return $pageStr;
}
/**
* construct_Pages 生成頁(yè)碼數(shù)組
* 鍵為頁(yè)碼,值為鏈接
* $this->page_arr=Array(
* [1] => index.php?page=1
* [2] => index.php?page=2
* [3] => index.php?page=3
* ......)
*/
protected function construct_Pages(){
//計(jì)算總頁(yè)數(shù)
$this->countPages=ceil($this->count/$this->subPages);
//根據(jù)當(dāng)前頁(yè)計(jì)算前后頁(yè)數(shù)
$leftPage_num=floor($this->showPages/2);
$rightPage_num=$this->showPages-$leftPage_num;
//左邊顯示數(shù)為當(dāng)前頁(yè)減左邊該顯示的數(shù) 例如總顯示7頁(yè) 當(dāng)前頁(yè)是5 左邊最小為5-3 右邊為5+3
$left=$this->currPage-$leftPage_num;
$left=max($left,1); //左邊最小不能小于1
$right=$left+$this->showPages-1; //左邊加顯示頁(yè)數(shù)減1就是右邊顯示數(shù)
$right=min($right,$this->countPages); //右邊最大不能大于總頁(yè)數(shù)
$left=max($right-$this->showPages+1,1); //確定右邊再計(jì)算左邊,必須二次計(jì)算
for ($i=$left; $i <= $right; $i++) {
$_GET['page'] = $i;
$this->page_arr[$i]=$this->href.'?'.http_build_query($_GET);
}
}
}
?>
用法示例demo.php:
<?php
/**
* demo
*/
header("content-type:text/html;charset=utf8");
include('Page.class.php'); //引入類(lèi)
//$p=new Page(總條數(shù),顯示頁(yè)數(shù),當(dāng)前頁(yè)碼,每頁(yè)顯示條數(shù),[鏈接]);
//連接不設(shè)置則為當(dāng)前鏈接
$page=isset($_GET['page']) ? $_GET['page'] : 1;
$p=new Page(100,4,$page,8);
//生成一個(gè)頁(yè)碼數(shù)組(鍵為頁(yè)碼,值為鏈接)
echo "<pre>";
print_r($p->getPages());
//樣式 共45條記錄,每頁(yè)顯示10條,當(dāng)前第1/4頁(yè) [首頁(yè)] [上頁(yè)] [1] [2] [3] .. [下頁(yè)] [尾頁(yè)]
echo $p->showPages(1);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》、《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP+JavaScript實(shí)現(xiàn)無(wú)刷新上傳圖片
本文主要介紹了PHP+JavaScript實(shí)現(xiàn)無(wú)刷新上傳圖片的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
PHP函數(shù)rtrim()使用中的怪異現(xiàn)象分析
這篇文章主要介紹了PHP函數(shù)rtrim()使用中的怪異現(xiàn)象,結(jié)合具體實(shí)例形式分析了php函數(shù)rtrim在進(jìn)行字符匹配過(guò)程中出現(xiàn)的問(wèn)題與解決方法,有助于進(jìn)一步了解rtrim函數(shù)的原理與使用技巧,需要的朋友可以參考下2017-02-02
生成靜態(tài)頁(yè)面的php函數(shù),php愛(ài)好者站推薦
生成靜態(tài)頁(yè)面的php函數(shù),php愛(ài)好者站推薦...2007-03-03
解析PHP中的內(nèi)存管理,PHP動(dòng)態(tài)分配和釋放內(nèi)存
本篇文章是對(duì)PHP中的內(nèi)存管理,PHP動(dòng)態(tài)分配和釋放內(nèi)存進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法
這篇文章主要介紹了PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法,包括了curl的使用及cookie的操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
php中判斷文件存在是用file_exists還是is_file的整理
在寫(xiě)程序時(shí)發(fā)現(xiàn)在判斷文件是否存在時(shí),有兩種寫(xiě)法,有的人用了is_file,有的人用了file_exists,用哪個(gè)更好或者說(shuō)更合適呢2012-09-09
使用PHP和RabbitMQ實(shí)現(xiàn)消息隊(duì)列功能
這篇文章主要介紹了如何使用PHP和RabbitMQ實(shí)現(xiàn)消息隊(duì)列功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PHP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03

