分頁類一
復制代碼 代碼如下:
<?php
/**
分頁類
修改:Silence
Creatdate:2006-5-30
LastModify:2009-5-31
使用方法
$page = new page ( $result, 20 ); //$result為返回記錄集數組 ,20為返回每頁條數
$index = $page->GetIndexBar () . $page->GetPageInfo ();
print_r ( $result );
echo \"<br><br>\";
echo \"<center>\".$index.\"</center>\";
*/
class Page {
private $mTotalRowsNum = 0; // 總信息行數
private $mCurPageNumber = 1; // 當前所在頁
private $mTotalPagesNum = 1; // 總頁數
private $mQueryString; // 頁面傳遞的數據(url?後的字符串)
private $mPageRowsNum = 20; // 每頁顯示行數
private $mIndexBarLength = 11; // 索引條的頁數
private $mIndexBar = ''; // 頁碼索引條
private $mPageInfo = ''; // 分頁信息
// 頁碼索引條樣式
private $mNextButton = \"<font style=\\"font-family:webdings\\">8</font>\";
private $mPreButton = \"<font style=\\"font-family:webdings\\">7</font>\";
private $mFirstButton = \"<font style=\\"font-family:webdings\\">9</font>\";
private $mLastButton = \"<font style=\\"font-family:webdings\\">:</font>\";
private $mCssIndexBarCurPage = \"font-weight:bold;color:#FF0000\";
private $mCssIndexBarPage = '';
// 分頁信息樣式
private $mCssPageInfoNumFont = 'color:#FF0000';
private $mCssPageInfoFont = '';
// 構造方法
public function __construct(&$rSqlQuery, $userPageRowsNum = '') {
if (! is_array ( $rSqlQuery )) {
$this->SetDbPageBreak ( $rSqlQuery, $userPageRowsNum );
} else {
$this->SetArrayPageBreak ( $rSqlQuery, $userPageRowsNum );
}
}
// 設置數據庫型分頁
private function SetDbPageBreak(&$rSqlQuery, $userPageRowsNum = '') {
$this->SetDbTotalRowsNum ( $rSqlQuery );
$this->SetTotalPagesNum ( $userPageRowsNum );
if ($this->mTotalPagesNum > 1) {
$this->SetCurPageNumber ();
$this->SetSqlQuery ( $rSqlQuery );
$this->SetQueryString ();
$this->SetIndexBar ();
$this->SetPageInfo ();
}
}
// 設置數組型分頁
private function SetArrayPageBreak(&$rArray, $userPageRowsNum = '', $userTotalRowsNum = '') {
$this->SetArrayTotalRowsNum ( $rArray, $userTotalRowsNum );
$this->SetTotalPagesNum ( $userPageRowsNum );
if ($this->mTotalPagesNum > 1) {
$this->SetCurPageNumber ();
$this->SetArray ( $rArray );
$this->SetQueryString ();
$this->SetIndexBar ();
$this->SetPageInfo ();
}
}
// 數據庫型計算總行數
private function SetDbTotalRowsNum($rSqlQuery) {
$this->mTotalRowsNum = mysql_num_rows ( mysql_query ( $rSqlQuery ) );
}
// 數組型計算總行數
private function SetArrayTotalRowsNum($array) {
$this->mTotalRowsNum = count ( $array );
}
// 計算總頁數
private function SetTotalPagesNum($userPageRowsNum = '') {
if ($userPageRowsNum) {
$this->mPageRowsNum = $userPageRowsNum;
}
$this->mTotalPagesNum = ( int ) (floor ( ($this->mTotalRowsNum - 1) / $this->mPageRowsNum ) + 1);
}
// 計算當前頁數
private function SetCurPageNumber() {
if ($_GET ['page']) {
$this->mCurPageNumber = $_GET ['page'];
}
}
// 修正Sql截取語句
private function SetSqlQuery(&$rSqlQuery) {
$start_number = ($this->mCurPageNumber - 1) * $this->mPageRowsNum;
$rSqlQuery .= \" LIMIT \" . $start_number . \",\" . $this->mPageRowsNum;
}
// 修正截取後的Array
private function SetArray(&$rArray) {
$start_number = ($this->mCurPageNumber - 1) * $this->mPageRowsNum;
$rArray = array_slice ( $rArray, $start_number, $this->mPageRowsNum );
}
// 修正 $_GET 傳遞數據
private function SetQueryString() {
$query_string = $_SERVER ['QUERY_STRING'];
if ($query_string == '') {
$this->mQueryString = \"?page=\";
} else {
$this->mQueryString = preg_replace ( \"/&?page=\d+/\", '', $query_string );
$this->mQueryString = \"?\" . $this->mQueryString . \"&page=\";
}
}
// 設置頁碼索引條
private function GetPageIndex() {
if ($this->mTotalPagesNum <= $this->mIndexBarLength) {
$first_number = 1;
$last_number = $this->mTotalPagesNum;
} else {
$offset = ( int ) floor ( $this->mIndexBarLength / 2 );
if (($this->mCurPageNumber - $offset) <= 1) {
$first_number = 1;
} elseif (($this->mCurPageNumber + $offset) > $this->mTotalPagesNum) {
$first_number = $this->mTotalPagesNum - $this->mIndexBarLength + 1;
} else {
$first_number = $this->mCurPageNumber - $offset;
}
$last_number = $first_number + $this->mIndexBarLength - 1;
}
$last_number;
for($i = $first_number; $i <= $last_number; $i ++) {
if ($this->mCurPageNumber == $i) {
$page_index .= \"<font style='\" . $this->mCssIndexBarCurPage . \"'>\" . $i . \"</font> \";
} else {
$page_index .= \" <a href='\" . $this->mQueryString . $i . \"' style='\" . $this->mCssIndexBarPage . \"'>\" . $i . \"</a> \";
}
}
return $page_index;
}
// 設置頁碼索引條
private function SetIndexBar() {
$this->mIndexBar = $this->GetNavFirstButton ();
$this->mIndexBar .= $this->GetNavPreButton ();
$this->mIndexBar .= $this->GetPageIndex ();
$this->mIndexBar .= $this->GetNavNextButton ();
$this->mIndexBar .= $this->GetNavLastButton ();
}
// 得到頁碼索引條 首頁按鈕
private function GetNavFirstButton() {
return \"<a href='\" . $this->mQueryString . \"1'>\" . $this->mFirstButton . \"</a> \";
}
// 得到頁碼索引條 上一頁按鈕
private function GetNavPreButton() {
if ($this->mCurPageNumber > 1) {
$pre_number = $this->mCurPageNumber - 1;
} else {
$pre_number = 1;
}
return \"<a href='\" . $this->mQueryString . $pre_number . \"'>\" . $this->mPreButton . \"</a> \";
}
// 得到頁碼索引條 下一頁按鈕
private function GetNavNextButton() {
if ($this->mCurPageNumber < $this->mTotalPagesNum) {
$next_number = $this->mCurPageNumber + 1;
} else {
$next_number = $this->mTotalPagesNum;
}
return \"<a href='\" . $this->mQueryString . $next_number . \"'>\" . $this->mNextButton . \"</a> \";
}
// 得到頁碼索引條 末頁按鈕
private function GetNavLastButton() {
return \"<a href='\" . $this->mQueryString . $this->mTotalPagesNum . \"'>\" . $this->mLastButton . \"</a> \";
}
// 設置分頁信息
private function SetPageInfo() {
$this->mPageInfo = \"<font style='\" . $this->mCssPageInfoFont . \"'>\";
$this->mPageInfo .= \"共 <font style='\" . $this->mCssPageInfoNumFont . \"'>\" . $this->mTotalRowsNum . \"</font> 條信息 | \";
$this->mPageInfo .= \"<font style='\" . $this->mCssPageInfoNumFont . \"'>\" . $this->mPageRowsNum . \"</font> 條/頁 | \";
$this->mPageInfo .= \"共 <font style='\" . $this->mCssPageInfoNumFont . \"'>\" . $this->mTotalPagesNum . \"</font> 頁 | \";
$this->mPageInfo .= \"第 <font style='\" . $this->mCssPageInfoNumFont . \"'>\" . $this->mCurPageNumber . \"</font> 頁\";
$this->mPageInfo .= \"</font>\";
}
// 取出頁碼索引條
public function GetIndexBar() {
return $this->mIndexBar;
}
// 取出分頁信息
public function GetPageInfo() {
return $this->mPageInfo;
}
//釋放類
function __destruct() {
}
}
?>
分頁類二
復制代碼 代碼如下:
<?php
/*
* Created on 2007-6-8
* Programmer : Alan , Msn - [email protected]
* KeBeKe.com Develop a project PHP - MySQL - Apache
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
//為了避免重復包含文件而造成錯誤,加了判斷函數是否存在的條件:
if(!function_exists(pageft)){
//定義函數pageft(),三個參數的含義為:
//$totle:信息總數;
//$displaypg:每頁顯示信息數,這裡設置為默認是20;
//$url:分頁導航中的鏈接,除了加入不同的查詢信息“page”外的部分都與這個URL相同。
// 默認值本該設為本頁URL(即$_SERVER["REQUEST_URI"]),但設置默認值的右邊只能為常量,所以該默認值設為空字符串,在函數內部再設置為本頁URL。
function pageft($totle,$displaypg=20,$url=''){
//定義幾個全局變量:
//$page:當前頁碼;
//$firstcount:(數據庫)查詢的起始項;
//$pagenav:頁面導航條代碼,函數內部並沒有將它輸出;
//$_SERVER:讀取本頁URL“$_SERVER["REQUEST_URI"]”所必須。
global $page,$firstcount,$pagenav,$_SERVER;
//為使函數外部可以訪問這裡的“$displaypg”,將它也設為全局變量。注意一個變量重新定義為全局變量後,原值被覆蓋,所以這裡給它重新賦值。
$GLOBALS["displaypg"]=$displaypg;
if(!$page) $page=1;
//如果$url使用默認,即空值,則賦值為本頁URL:
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
//URL分析:
$parse_url=parse_url($url);
$url_query=$parse_url["query"]; //單獨取出URL的查詢字串
if($url_query){
//因為URL中可能包含了頁碼信息,我們要把它去掉,以便加入新的頁碼信息。
//這裡用到了正則表達式,請參考“PHP中的正規表達式”
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
//將處理後的URL的查詢字串替換原來的URL的查詢字串:
$url=str_replace($parse_url["query"],$url_query,$url);
//在URL後加page查詢信息,但待賦值:
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}
//頁碼計算:
$lastpg=ceil($totle/$displaypg); //最後頁,也是總頁數
$page=min($lastpg,$page);
$prepg=$page-1; //上一頁
$nextpg=($page==$lastpg ? 0 : $page+1); //下一頁
$firstcount=($page-1)*$displaypg;
//開始分頁導航條代碼:
$pagenav="顯示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 條記錄,共 $totle 條記錄";
//如果只有一頁則跳出函數:
if($lastpg<=1) return false;
$pagenav.=" <a href='$url=1'>首頁</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg'>前頁</a> "; else $pagenav.=" 前頁 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg'>後頁</a> "; else $pagenav.=" 後頁 ";
$pagenav.=" <a href='$url=$lastpg'>尾頁</a> ";
//下拉跳轉列表,循環列出所有頁碼:
$pagenav.=" 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
else $pagenav.="<option value='$i'>$i</option>\n";
}
$pagenav.="</select> 頁,共 $lastpg 頁";
}
}
//-------------------------具體演示舉例-------------------------------
/*
//(前面程序略)
include("pageft.php"); //包含“pageft.php”文件
//取得總信息數
$result=mysql_query("select * from mytable");
$total=mysql_num_rows($result);
//調用pageft(),每頁顯示10條信息(使用默認的20時,可以省略此參數),使用本頁URL(默認,所以省略掉)。
pageft($total,10);
//現在產生的全局變量就派上用場了:
$result=mysql_query("select * from mytable limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){
//(列表內容略)
}
//輸出分頁導航條代碼:
echo $pagenav;
//(後面程序略)
*/
?>
分頁類系列之三
復制代碼 代碼如下:
<?php
class Page {
private $total; //查詢所有的數據總記錄數
private $page; //當前第幾頁
private $num; //每頁顯示記錄的條數
private $pageNum; //一共多少頁
private $offset; //從數據庫中取記錄的開始偏移數
function __construct($total, $page=1, $num=5) {
$this->total=$total;
$this->page=$page;
$this->num=$num;
$this->pageNum=$this->getPageNum();
$this->offset=$this->getOffset();
}
private function getPageNum(){
return ceil($this->total/$this->num);
}
private function getNextPage() {
if($this->page==$this->pageNum)
return false;
else
return $this->page+1;
}
private function getPrevPage() {
if($this->page==1)
return false;
else
return $this->page-1;
}
//數據庫查詢的偏移量
private function getOffset() {
return ($this->page-1)*$this->num;
}
//當前頁開始的記錄數
private function getStartNum() {
if($this->total==0)
return 0;
else
return $this->offset+1;
}
//當前頁結束的記錄數
private function getEndNum() {
return min($this->offset+$this->num,$this->total);
}
public function getPageInfo(){
$pageInfo=array(
"row_total" => $this->total,
"row_num" => $this->num,
"page_num" => $this->getPageNum(),
"current_page" => $this->page,
"row_offset" => $this->getOffset(),
"next_page" => $this->getNextPage(),
"prev_page" => $this->getPrevPage(),
"page_start" => $this->getStartNum(),
"page_end" => $this->getEndNum()
);
return $pageInfo;
}
}
?>
3種分頁類使用起來效率都不錯,也都很簡單,這裡推薦給大家。