PHP分页完美参考
轉(zhuǎn)自http://www.knowsky.com/3187.html
1、前言
分頁(yè)顯示是一種非常常見(jiàn)的瀏覽和顯示大量數(shù)據(jù) 的方法,屬于web編程中最常處理的事件之一。對(duì)于web編程的老手來(lái)說(shuō),編寫(xiě)這種代碼實(shí)在是和呼吸一樣自然,但是對(duì)于初學(xué)者來(lái)說(shuō),常常對(duì)這個(gè)問(wèn)題摸不著 頭緒,因此特地撰寫(xiě)此文對(duì)這個(gè)問(wèn)題進(jìn)行詳細(xì)的講解,力求讓看完這篇文章的朋友在看完以后對(duì)于分頁(yè)顯示的原理和實(shí)現(xiàn)方法有所了解。本文適合初學(xué)者閱讀,所有 示例代碼均使用php編寫(xiě)。
2、原理
所謂分頁(yè)顯示,也就是將數(shù)據(jù)庫(kù)中的結(jié)果集人為的分成一段一段的來(lái)顯示,這里需要兩個(gè)初始的參數(shù):
每頁(yè)多少條記錄($PageSize)?
當(dāng)前是第幾頁(yè)($CurrentPageID)?
現(xiàn)在只要再給我一個(gè)結(jié)果集,我就可以顯示某段特定的結(jié)果出來(lái)。
至于其他的參數(shù),比如:上一頁(yè)($PReviousPageID)、下一頁(yè)($NextPageID)、總頁(yè)數(shù)($numPages)等等,都可以根據(jù)前邊這幾個(gè)東西得到。
以MySQL數(shù)據(jù)庫(kù)為例,如果要從表內(nèi)截取某段內(nèi)容,sql語(yǔ)句可以用:select * from table limit offset, rows。看看下面一組sql語(yǔ)句,嘗試一下發(fā)現(xiàn)其中的規(guī)率。
前10條記錄:select * from table limit 0,10
第11至20條記錄:select * from table limit 10,10
第21至30條記錄:select * from table limit 20,10
……
這一組sql語(yǔ)句其實(shí)就是當(dāng)$PageSize=10的時(shí)候取表內(nèi)每一頁(yè)數(shù)據(jù)的sql語(yǔ)句,我們可以總結(jié)出這樣一個(gè)模板:
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
拿這個(gè)模板代入對(duì)應(yīng)的值和上邊那一組sql語(yǔ)句對(duì)照一下看看是不是那么回事。搞定了最重要的如何獲取數(shù)據(jù)的問(wèn)題以后,剩下的就僅僅是傳遞參數(shù),構(gòu)造合適的sql語(yǔ)句然后使用php從數(shù)據(jù)庫(kù)內(nèi)獲取數(shù)據(jù)并顯示了。以下我將用具體代碼加以說(shuō)明。
3、簡(jiǎn)單代碼
請(qǐng)?jiān)敿?xì)閱讀以下代碼,自己調(diào)試運(yùn)行一次,最好把它修改一次,加上自己的功能,比如搜索等等。
<?php
// 建立數(shù)據(jù)庫(kù)連接
$link = mysql_connect("localhost", "mysql_user", "mysql_passWord")
????? or die("Could not connect: " . mysql_error());
// 獲取當(dāng)前頁(yè)數(shù)
if( isset($_GET['page']) ){
?? $page = intval( $_GET['page'] );
}
else{
?? $page = 1;
}
// 每頁(yè)數(shù)量
$PageSize = 10;
// 獲取總數(shù)據(jù)量
$sql = "select count(*) as amount from table";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$amount = $row['amount'];
// 記算總共有多少頁(yè)
if( $amount ){
?? if( $amount < $page_size ){ $page_count = 1; }?????????????? //如果總數(shù)據(jù)量小于$PageSize,那么只有一頁(yè)
?? if( $amount % $page_size ){????????????????????????????????? //取總數(shù)據(jù)量除以每頁(yè)數(shù)的余數(shù)
?????? $page_count = (int)($amount / $page_size) + 1;?????????? //如果有余數(shù),則頁(yè)數(shù)等于總數(shù)據(jù)量除以每頁(yè)數(shù)的結(jié)果取整再加一
?? }else{
?????? $page_count = $amount / $page_size;????????????????????? //如果沒(méi)有余數(shù),則頁(yè)數(shù)等于總數(shù)據(jù)量除以每頁(yè)數(shù)的結(jié)果
?? }
}
else{
?? $page_count = 0;
}
// 翻頁(yè)鏈接
$page_string = '';
if( $page == 1 ){
?? $page_string .= '第一頁(yè)|上一頁(yè)|';
}
else{
?? $page_string .= '<a href=?page=1>第一頁(yè)</a>|<a href=?page='.($page-1).'>上一頁(yè)</a>|';
}
if( ($page == $page_count) || ($page_count == 0) ){
?? $page_string .= '下一頁(yè)|尾頁(yè)';
}
else{
?? $page_string .= '<a href=?page='.($page+1).'>下一頁(yè)</a>|<a href=?page='.$page_count.'>尾頁(yè)</a>';
}
// 獲取數(shù)據(jù),以二維數(shù)組格式返回結(jié)果
if( $amount ){
?? $sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";
?? $result = mysql_query($sql);
??
?? while ( $row = mysql_fetch_row($result) ){
?????? $rowset[] = $row;
?? }
}else{
?? $rowset = array();
}
// 沒(méi)有包含顯示結(jié)果的代碼,那不在討論范圍,只要用foreach就可以很簡(jiǎn)單的用得到的二維數(shù)組來(lái)顯示結(jié)果
?>
4、OO風(fēng)格代碼
以下代碼中的數(shù)據(jù)庫(kù)連接是使用的pear db類(lèi)進(jìn)行處理
<?php
// FileName: Pager.class.php
// 分頁(yè)類(lèi),這個(gè)類(lèi)僅僅用于處理數(shù)據(jù)結(jié)構(gòu),不負(fù)責(zé)處理顯示的工作
Class Pager
{
?? var $PageSize;???????????? //每頁(yè)的數(shù)量
?? var $CurrentPageID;??????? //當(dāng)前的頁(yè)數(shù)
?? var $NextPageID;?????????? //下一頁(yè)
?? var $PreviousPageID;?????? //上一頁(yè)
?? var $numPages;???????????? //總頁(yè)數(shù)
?? var $numItems;???????????? //總記錄數(shù)
?? var $isFirstPage;????????? //是否第一頁(yè)
?? var $isLastPage;?????????? //是否最后一頁(yè)
?? var $sql;????????????????? //sql查詢語(yǔ)句
??
? function Pager($option)
?? {
?????? global $db;
?????? $this->_setOptions($option);
?????? // 總條數(shù)
?????? if ( !isset($this->numItems) )
?????? {
?????????? $res = $db->query($this->sql);
?????????? $this->numItems = $res->numRows();
?????? }
?????? // 總頁(yè)數(shù)
?????? if ( $this->numItems > 0 )
?????? {
?????????? if ( $this->numItems < $this->PageSize ){ $this->numPages = 1; }
?????????? if ( $this->numItems % $this->PageSize )
?????????? {
?????????????? $this->numPages= (int)($this->numItems / $this->PageSize) + 1;
?????????? }
?????????? else
?????????? {
?????????????? $this->numPages = $this->numItems / $this->PageSize;
?????????? }
?????? }
?????? else
?????? {
?????????? $this->numPages = 0;
?????? }
??????
?????? switch ( $this->CurrentPageID )
?????? {
?????????? case $this->numPages == 1:
?????????????? $this->isFirstPage = true;
?????????????? $this->isLastPage = true;
?????????????? break;
?????????? case 1:
?????????????? $this->isFirstPage = true;
?????????????? $this->isLastPage = false;
?????????????? break;
?????????? case $this->numPages:
?????????????? $this->isFirstPage = false;
?????????????? $this->isLastPage = true;
?????????????? break;
?????????? default:
?????????????? $this->isFirstPage = false;
?????????????? $this->isLastPage = false;
?????? }
??????
?????? if ( $this->numPages > 1 )
?????? {
?????????? if ( !$this->isLastPage ) { $this->NextPageID = $this->CurrentPageID + 1; }
?????????? if ( !$this->isFirstPage ) { $this->PreviousPageID = $this->CurrentPageID - 1; }
?????? }
??????
?????? return true;
?? }
??
?? /***
??? *
??? * 返回結(jié)果集的數(shù)據(jù)庫(kù)連接
??? * 在結(jié)果集比較大的時(shí)候可以直接使用這個(gè)方法獲得數(shù)據(jù)庫(kù)連接,然后在類(lèi)之外遍歷,這樣開(kāi)銷(xiāo)較小
??? * 如果結(jié)果集不是很大,可以直接使用getPageData的方式獲取二維數(shù)組格式的結(jié)果
??? * getPageData方法也是調(diào)用本方法來(lái)獲取結(jié)果的
??? *
??? ***/
??
?? function getDataLink()
?? {
?????? if ( $this->numItems )
?????? {
?????????? global $db;
??????????
?????????? $PageID = $this->CurrentPageID;
??????????
?????????? $from = ($PageID - 1)*$this->PageSize;
?????????? $count = $this->PageSize;
?????????? $link = $db->limitQuery($this->sql, $from, $count);?? //使用Pear DB::limitQuery方法保證數(shù)據(jù)庫(kù)兼容性
??????????
?????????? return $link;
?????? }
?????? else
?????? {
?????????? return false;
?????? }
?? }
??
?? /***
??? *
??? * 以二維數(shù)組的格式返回結(jié)果集
??? *
??? ***/
??
?? function getPageData()
?? {
?????? if ( $this->numItems )
?????? {
?????????? if ( $res = $this->getDataLink() )
?????????? {??????
?????????????? if ( $res->numRows() )
?????????????? {
?????????????????? while ( $row = $res->fetchRow() )
?????????????????? {
?????????????????????? $result[] = $row;
?????????????????? }
?????????????? }
?????????????? else
?????????????? {
?????????????????? $result = array();
?????????????? }
??????????????
?????????????? return $result;
?????????? }
?????????? else
?????????? {
?????????????? return false;
?????????? }
?????? }
?????? else
?????? {
?????????? return false;
?????? }
?? }
??
?? function _setOptions($option)
?? {
?????? $allow_options = array(
?????????????????? 'PageSize',
?????????????????? 'CurrentPageID',
?????????????????? 'sql',
?????????????????? 'numItems'
?????? );
??????
?????? foreach ( $option as $key => $value )
?????? {
?????????? if ( in_array($key, $allow_options) && ($value != null) )
?????????? {
?????????????? $this->$key = $value;
?????????? }
?????? }
??????
?????? return true;
?? }
}
?>
<?php
// FileName: test_pager.php
// 這是一段簡(jiǎn)單的示例代碼,前邊省略了使用pear db類(lèi)建立數(shù)據(jù)庫(kù)連接的代碼
require "Pager.class.php";
if ( isset($_GET['page']) )
{
?? $page = (int)$_GET['page'];
}
else
{
?? $page = 1;
}
$sql = "select * from table order by id";
$pager_option = array(
?????? "sql" => $sql,
?????? "PageSize" => 10,
?????? "CurrentPageID" => $page
);
if ( isset($_GET['numItems']) )
{
?? $pager_option['numItems'] = (int)$_GET['numItems'];
}
$pager = @new Pager($pager_option);
$data = $pager->getPageData();
if ( $pager->isFirstPage )
{
?? $turnover = "首頁(yè)|上一頁(yè)|";
}
else
{
?? $turnover = "<a href='?page=1&numItems=".$pager->numItems."'>首頁(yè)</a>|< a href='?page=".$pager->PreviousPageID."&numItems=".$pager-> numItems."'>上一頁(yè)</a>|";
}
if ( $pager->isLastPage )
{
?? $turnover .= "下一頁(yè)|尾頁(yè)";
}
else
{
?? $turnover .= "<a href='?page=".$pager->NextPageID."&numItems=".$pager-> numItems."'>下一頁(yè)</a>|<a href='?page=".$pager->numPages."&numItems=".$pager-> numItems."'>尾頁(yè)</a>";
}
?>
?
需要說(shuō)明的地方有兩個(gè):
這個(gè)類(lèi)僅僅處理數(shù)據(jù),并不負(fù)責(zé)處理顯示,因?yàn)槲矣X(jué)得將數(shù)據(jù)的處理和結(jié)果的顯示都放到一個(gè)類(lèi)里邊實(shí)在是有些勉強(qiáng)。顯示的時(shí)候情況和要求多變,不如自己根據(jù)類(lèi)給出的結(jié)果處理,更好的方法是根據(jù)這個(gè)Pager類(lèi)繼承一個(gè)自己的子類(lèi)來(lái)顯示不同的分頁(yè),比如顯示用戶分頁(yè)列表可以:
<?php
Class MemberPager extends Pager
{
?? function showMemberList()
?? {
?????? global $db;
??????
?????? $data = $this->getPageData();
// 顯示結(jié)果的代碼
?????? // ......
?? }
}
/// 調(diào)用
if ( isset($_GET['page']) )
{
?? $page = (int)$_GET['page'];
}
else
{
?? $page = 1;
}
$sql = "select * from members order by id";
$pager_option = array(
?????? "sql" => $sql,
?????? "PageSize" => 10,
?????? "CurrentPageID" => $page
);
if ( isset($_GET['numItems']) )
{
?? $pager_option['numItems'] = (int)$_GET['numItems'];
}
$pager = @new MemberPager($pager_option);
$pager->showMemberList();
?>
?
第二個(gè)需要說(shuō)明的地方就是不同數(shù)據(jù)庫(kù)的兼容性,在不同的數(shù)據(jù)庫(kù)里截獲一段結(jié)果的寫(xiě)法是不一樣的。
mysql: select * from table limit offset, rows
pgsql: select * from table limit m offset n
......
所以要在類(lèi)里邊獲取結(jié)果的時(shí)候需要使用pear db類(lèi)的limitQuery方法。
ok,寫(xiě)完收功,希望花時(shí)間看完這些文字的你不覺(jué)得是浪費(fèi)了時(shí)間。
轉(zhuǎn)載于:https://www.cnblogs.com/caroar/archive/2012/12/12/2814649.html
總結(jié)
- 上一篇: 汽车厂商急定25片晶圆 台积电嘲讽:难怪
- 下一篇: javascript php 之间传递