?
<?
php
/*
?*?分頁(yè)顯示類(lèi)
?*?PageItem.php v?1.0.0
?*?編程:Boban<boban@21php.com>
?*?討論:http://www.21php.com/forums/
?*?更新:2004-02-02
?*?說(shuō)明:
?*?1.?配合MYSQL數(shù)據(jù)庫(kù)使用
?*?2.?類(lèi)沒(méi)有提供連接數(shù)據(jù)庫(kù)的功能,需在外部建立數(shù)據(jù)庫(kù)連接。
?*?
*/
/*
?*?使用方法:
?*?$sql?=?"select?*?from?news?limit?0,10";
?*?$hdc?=?new?PageItem($sql);
?*?echo?$hdc->myPageItem();
?*?$arrRecords?=?$hdc->ReadList();
?*?
*/
if
?(
!
defined
(
"
__PAGEITEM__
"
))?{
????
define
(
"
__PAGEITEM__
"
,
?
1
);
}?
else
?{
????
exit
(
3
);
}?

class
?PageItem?{
????
var
?
$iDefaultRecords
?
=
?
10
;?
//
?默認(rèn)每頁(yè)顯示記錄數(shù),如果沒(méi)有設(shè)置,就使用默認(rèn)
值
????
var
?
$iMaxRecord
;?
//
每頁(yè)記錄數(shù)
????
var
?
$iTotal
;?
//
記錄總數(shù)
????
var
?
$sqlRecord
;?
//
?獲取記錄的SQL查詢(xún)
????
var
?
$iPages
;?
//
總頁(yè)數(shù)
????
var
?
$CPages
;?
//
當(dāng)前頁(yè)數(shù)
????
/*
??*?構(gòu)造函數(shù)?--?初始化變量
??*?參數(shù):SQL查詢(xún)語(yǔ)句,將忽略LIMIT語(yǔ)句
??*?
*/
????
function
?PageItem(
$sql
?
=
?
""
)
????{?
????????
//
?register_shutdown_function($this->_PageItem());
????????
$this
->
SetMaxRecord(
$this
->
iDefaultRecords);
????????
/*
???????*?解析SQL語(yǔ)句
???????*?
*/
????????
if
?(
$sql
?
<>
?
""
)?{
????????????
list
(
$sql
)?
=
?
spliti
(
"
LIMIT
"
,
?
$sql
);?
//
?去除LIMIT語(yǔ)句
????????????
$this
->
sqlRecord?
=
?
trim
(
$sql
);
????????????
list
(
,
?
$sql
)?
=
?
spliti
(
"
FROM
"
,
?
$sql
);
????????????
$sql
?
=
?
trim
(
$sql
);
???
if
(
preg_match
?(
"
/\bGROUP\b?\bBY\b/i
"
,
?
$sql
))
???{
????
//
echo?"HAVE?GROUP?BY";
????
if
(
preg_match
?(
"
/\bHAVING\b/i
"
,
?
$sql
))?
list
(
,
$field
)?
=
?
spliti
(
"
HAVING
"
,
$sql
);
????
list
(
$field
)?
=
?
spliti
(
'
?
'
,
trim
(
$field
));
????
//
echo?$field;
????
$this
->
iTotal?
=
?
$this
->
CountRecord(
"
SELECT?$field,COUNT(DISTINCT?$field)?

AS?cnt?FROM?
"
?
.
?
$sql
,
2
);
???}
???
else
?
$this
->
iTotal?
=
?
$this
->
CountRecord(
"
SELECT?COUNT(*)?AS?cnt?FROM?
"
?
.
?

$sql
,
1
);
????????}?
????????
$this
->
iPages?
=
?
ceil
(
$this
->
iTotal?
/
?
$this
->
iMaxRecord);
????????
$this
->
CPages?
=
?
$_REQUEST
[
'
page
'
];
????????
if
?(
$this
->
CPages?
<=
?
0
)?
$this
->
CPages?
=
?
1
;
????????
if
?(
$this
->
CPages?
>
?
$this
->
iPages)?
$this
->
CPages?
=
?
$this
->
iPages;
????????
//
echo?"SELECT?COUNT(*)?AS?cnt?FROM?"?.?$sql;
??//echo?$this->iTotal;
????}?
????
/*
?????*?析構(gòu)函數(shù)?--?暫時(shí)不可用
?????*?
*/
????
function
?_PageItem()
????{?
????????
//
?$this->linkid?=?NULL;
????}?

????
function
?SetMaxRecord(
$cnt
)
????{
????????
$this
->
iMaxRecord?
=
?
$cnt
;
????}?

????
/*
?????*?統(tǒng)計(jì)匹配的記錄總數(shù)
?????*?
*/
????
function
?CountRecord(
$sql
,
$type
)
????{
??
//
echo?$sql;
??
if
(
$type
?
==
?
1
)
??{
???
if
?((
$records
?
=
?
mysql_query
(
$sql
))?
&&
?(
$record
?
=
?
mysql_fetch_assoc
(
$records
)))?{
????
return
?
$record
[
'
cnt
'
];
???}?
else
?
return
?
0
;
??}
??
elseif
(
$type
?
==
?
2
)
??{
???
if
(
$records
?
=
?
mysql_query
(
$sql
))
????
return
?
mysql_affected_rows
();
??}
????}
?
/*
?
??*?讀取記錄
??*?
*/
?
function
?ReadList()
?{
??
$ret
?
=
?
array
();
??
$this
->
sqlRecord
.=
"
?LIMIT?
"
.
(
$this
->
CPages
-
1
)
*
$this
->
iMaxRecord
.
"
,
"
.
$this
-
>
iMaxRecord;
??
$records
?
=
?
mysql_query
(
$this
->
sqlRecord);
??
if
(
!
$records
)?
return
;
??
while
(
$record
?
=
?
mysql_fetch_array
(
$records
))
??{
???
$ret
[]?
=
?
$record
;
??}
??
return
?
$ret
;
?}

????
function
?LinktoPage(
$page
,
?
$msg
)
????{
????????
$link
?
=
?
$this
->
PageUrl(
$page
);
????????
return
?
"
<A?href=\
"
$link
\
"
>$msg</A>\n
"
;
????}?
????
function
?PageUrl(
$page
)
????{
????????
$phpself
?
=
?
"
http://
"
?
.
?
$_SERVER
[
'
SERVER_NAME
'
]?
.
?
$_SERVER
[
'
PHP_SELF
'
];
????????
$querystring
?
=
?
$_SERVER
[
'
QUERY_STRING
'
];
????????
$querystring
?
=
?
preg_replace
(
"
/page=[0-9]*&?/i
"
,
?
""
,
?
$querystring
);
????????
$link
?
=
?
$phpself
?
.
?
"
?page=$page&
"
?
.
?
$querystring
;
????????
return
?
$link
;
????}?

????
/*
?
??*?顯示當(dāng)前頁(yè)及總頁(yè)數(shù)???
??*?
*/
????
function
?PageNav()
????{
????????
echo
?
"
第
"
?
.
?
$this
->
CPages?
.
?
"
頁(yè)/共
"
?
.
?
$this
->
iPages?
.
?
"
頁(yè)
"
;
????}?

????
/*
?
???*?顯示翻頁(yè)按鈕,包括首頁(yè)、下頁(yè)、上頁(yè)、未頁(yè)
???*?
*/
????
function
?PageButton()
????{
????????
if
?(
$this
->
CPages?
>
?
1
)?{
????????????
echo
?
$this
->
LinktoPage(
1
,
?
"
首頁(yè)
"
);
????????????
echo
?
"
?|?
"
;
????????????
echo
?
$this
->
LinktoPage(
$this
->
CPages
-
1
,
?
"
上一頁(yè)
"
);
????????}?
else
?{
????????????
echo
?
"
首頁(yè)?|?上一頁(yè)
"
;
????????}?

????????
if
?(
$this
->
CPages?
<
?
$this
->
iPages)?{
????????????
echo
?
"
?|?
"
;
????????????
echo
?
$this
->
LinktoPage(
$this
->
CPages?
+
?
1
,
?
"
下一頁(yè)
"
);
????????????
echo
?
"
?|?
"
;
????????????
echo
?
$this
->
LinktoPage(
$this
->
iPages
,
?
"
首頁(yè)
"
);
????????}?
else
?{
????????????
echo
?
"
?|?下一頁(yè)?|?尾頁(yè)
"
;
????????}?
????}
????
/*
?
???*?顯示跳轉(zhuǎn)頁(yè)選擇框
???*?
*/
????
function
?SelectItem()
????{
????????
echo
?
"
跳到第<SELECT?name='topage'?size='1'?

onchange='window.location=this.value'>\n
"
;
????????
for
(
$i
?
=
?
1
;
$i
?
<=
?
$this
->
iPages;
$i
++
)?{
????????????
if
?(
$this
->
CPages?
==
?
$i
)
????????????????
$extra
?
=
?
"
selected
"
;
????????????
else
????????????????
$extra
?
=
?
""
;
????????????
echo
?
"
<OPTION?VALUE='
"
?
.
?
$this
->
PageUrl(
$i
)?
.
?
"
'?

$extra>$i</OPTION>
"
;
????????}?
????????
echo
?
"
</SELECT>\n
"
;
????}

????
/*
?????*?一次性顯示所有按鈕組件
?????*?
*/
????
function
?myPageItem()
????{
????????
$this
->
PageButton();
????????
$this
->
SelectItem();
????????
$this
->
PageNav();
????}?
}?
//
?類(lèi)結(jié)束
?>
???
<?php
/*
?* 分頁(yè)顯示類(lèi)
?* PageItem.php v 1.0.0
?* 編程:Boban<boban@21php.com>
?* 討論:http://www.21php.com/forums/
?* 更新:2004-02-02
?* 說(shuō)明:
?* 1. 配合MYSQL數(shù)據(jù)庫(kù)使用
?* 2. 類(lèi)沒(méi)有提供連接數(shù)據(jù)庫(kù)的功能,需在外部建立數(shù)據(jù)庫(kù)連接。
?* */
/*
?* 使用方法:
?* $sql = "select * from news limit 0,10";
?* $hdc = new PageItem($sql);
?* echo $hdc->myPageItem();
?* $arrRecords = $hdc->ReadList();
?* */
if (!defined("__PAGEITEM__")) {
??? define("__PAGEITEM__", 1);
} else {
??? exit(3);
}
class PageItem {
??? var $iDefaultRecords = 10; // 默認(rèn)每頁(yè)顯示記錄數(shù),如果沒(méi)有設(shè)置,就使用默認(rèn)
值
??? var $iMaxRecord; //每頁(yè)記錄數(shù)
??? var $iTotal; //記錄總數(shù)
??? var $sqlRecord; // 獲取記錄的SQL查詢(xún)
??? var $iPages; //總頁(yè)數(shù)
??? var $CPages; //當(dāng)前頁(yè)數(shù)
??? /*
? * 構(gòu)造函數(shù) -- 初始化變量
? * 參數(shù):SQL查詢(xún)語(yǔ)句,將忽略LIMIT語(yǔ)句
? * */
??? function PageItem($sql = "")
??? {
??????? // register_shutdown_function($this->_PageItem());
??????? $this->SetMaxRecord($this->iDefaultRecords);
??????? /*
?????? * 解析SQL語(yǔ)句
?????? * */
??????? if ($sql <> "") {
??????????? list($sql) = spliti("LIMIT", $sql); // 去除LIMIT語(yǔ)句
??????????? $this->sqlRecord = trim($sql);
??????????? list(, $sql) = spliti("FROM", $sql);
??????????? $sql = trim($sql);
?? if(preg_match ("/\bGROUP\b \bBY\b/i", $sql))
?? {
??? //echo "HAVE GROUP BY";
??? if(preg_match ("/\bHAVING\b/i", $sql)) list(,$field) = spliti
("HAVING",$sql);
??? list($field) = spliti(' ',trim($field));
??? //echo $field;
??? $this->iTotal = $this->CountRecord("SELECT $field,COUNT(DISTINCT $field)
AS cnt FROM " . $sql,2);
?? }
?? else $this->iTotal = $this->CountRecord("SELECT COUNT(*) AS cnt FROM " .
$sql,1);
??????? }
??????? $this->iPages = ceil($this->iTotal / $this->iMaxRecord);
??????? $this->CPages = $_REQUEST['page'];
??????? if ($this->CPages <= 0) $this->CPages = 1;
??????? if ($this->CPages > $this->iPages) $this->CPages = $this->iPages;
??????? //echo "SELECT COUNT(*) AS cnt FROM " . $sql;
? //echo $this->iTotal;
??? }
??? /*
???? * 析構(gòu)函數(shù) -- 暫時(shí)不可用
???? * */
??? function _PageItem()
??? {
??????? // $this->linkid = NULL;
??? }
??? function SetMaxRecord($cnt)
??? {
??????? $this->iMaxRecord = $cnt;
??? }
??? /*
???? * 統(tǒng)計(jì)匹配的記錄總數(shù)
???? * */
??? function CountRecord($sql,$type)
??? {
? //echo $sql;
? if($type == 1)
? {
?? if (($records = mysql_query($sql)) && ($record = mysql_fetch_assoc
($records))) {
??? return $record['cnt'];
?? } else return 0;
? }
? elseif($type == 2)
? {
?? if($records = mysql_query($sql))
??? return mysql_affected_rows();
? }
??? }
?/*
? * 讀取記錄
? * */
?function ReadList()
?{
? $ret = array();
? $this->sqlRecord.=" LIMIT ".($this->CPages-1)*$this->iMaxRecord.",".$this-
>iMaxRecord;
? $records = mysql_query($this->sqlRecord);
? if(!$records) return;
? while($record = mysql_fetch_array($records))
? {
?? $ret[] = $record;
? }
? return $ret;
?}
??? function LinktoPage($page, $msg)
??? {
??????? $link = $this->PageUrl($page);
??????? return "<A href=\"$link\">$msg</A>\n";
??? }
??? function PageUrl($page)
??? {
??????? $phpself = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
??????? $querystring = $_SERVER['QUERY_STRING'];
??????? $querystring = preg_replace("/page=[0-9]*&?/i", "", $querystring);
??????? $link = $phpself . "?page=$page&" . $querystring;
??????? return $link;
??? }
??? /*
? * 顯示當(dāng)前頁(yè)及總頁(yè)數(shù)??
? * */
??? function PageNav()
??? {
??????? echo "第" . $this->CPages . "頁(yè)/共" . $this->iPages . "頁(yè)";
??? }
??? /*
?? * 顯示翻頁(yè)按鈕,包括首頁(yè)、下頁(yè)、上頁(yè)、未頁(yè)
?? * */
??? function PageButton()
??? {
??????? if ($this->CPages > 1) {
??????????? echo $this->LinktoPage(1, "首頁(yè)");
??????????? echo " | ";
??????????? echo $this->LinktoPage($this->CPages-1, "上一頁(yè)");
??????? } else {
??????????? echo "首頁(yè) | 上一頁(yè)";
??????? }
??????? if ($this->CPages < $this->iPages) {
??????????? echo " | ";
??????????? echo $this->LinktoPage($this->CPages + 1, "下一頁(yè)");
??????????? echo " | ";
??????????? echo $this->LinktoPage($this->iPages, "首頁(yè)");
??????? } else {
??????????? echo " | 下一頁(yè) | 尾頁(yè)";
??????? }
??? }
??? /*
?? * 顯示跳轉(zhuǎn)頁(yè)選擇框
?? * */
??? function SelectItem()
??? {
??????? echo "跳到第<SELECT name='topage' size='1'
onchange='window.location=this.value'>\n";
??????? for($i = 1;$i <= $this->iPages;$i++) {
??????????? if ($this->CPages == $i)
??????????????? $extra = "selected";
??????????? else
??????????????? $extra = "";
??????????? echo "<OPTION VALUE='" . $this->PageUrl($i) . "'
$extra>$i</OPTION>";
??????? }
??????? echo "</SELECT>\n";
??? }
??? /*
???? * 一次性顯示所有按鈕組件
???? * */
??? function myPageItem()
??? {
??????? $this->PageButton();
??????? $this->SelectItem();
??????? $this->PageNav();
??? }
} // 類(lèi)結(jié)束
?>??