|
[PHP]
<?php
/*
PAGE CLASS
Copyright (C) 2004-2005 Harry Wang
GNU General Public License 321
This program is free software; you can
redistribute it and/or modify it under the
terms of the GNU General Public License as
published by the Free Software Foundation;
either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that
it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU
General Public License along with this
program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
/**
* 數據庫分頁?
* 這個數據庫分頁?可以使用在帶有ADODB的數據庫中
* @author Weizhen Wang <harry34010493@gmail.com>
* @version $Id: page.inc.php,v 1.9 2005/03/24 19:38:21 justin Exp $
* @example /inc/example/page.example.php
* 使用簡介:
* $p = new ShowPage(根据?($DB), 表名字, 分頁條數, 具体sql語句(可選)); //建立新對象
* $p->SetPageVar(pagecount); //設置翻頁的標示,默認是'p'
* $pVar = $p->GetPageVar(); //返回當前PageVar
* $p->GetTotalPageArray(); //生成下拉菜單
* $p->GetTotalPage(); //返回總頁數
* $p->GetLeftSide(); //返回向左的箭頭
* $p->GetRightSide(); //返回向右的箭頭
* $p->GetCurrentPage(); //返回當前頁面
* $p->GetLimit(); //返回每頁顯示條數
* $p->GetRowFrom(); //返回Select語句用的當前Row的位置 E.G."SELECT * FROM SupplierLogin LIMIT " . $p->GetRowFrom() . ", " . $p->GetLimit();
* $p->SetVar(); //設置同時需要傳送的參數
*/
class ShowPage
{
/**
* 左翻頁
* @var string
*/
var $outputLeftSide;
/**
* 右翻頁
* @var string
*/
var $outputRightSide;
var $file;
/**
* 翻頁參數
* @var string
*/
var $pVar = "p";
/**
* 當前頁
* @var int
*/
var $curr;
var $varstr;
var $tPage;
/**
* 數據庫對象
* @var DB
*/
var $DB;
/**
* 數據庫表名
* @var string
*/
var $table;
/**
* 每頁顯示的數據條數
* @var string
*/
var $limit;
/**
* SQL語句
* @var string
*/
var $sqlQuery;
var $tPageArray;
var $total;
var $tRow;
###############################################################################
/**
* 構造函數
* @param db $DB 數據庫對象,默認是ADODB
* @param string $table 數據庫表名
* @param int $limit 每頁顯示的數據條數
* @param string $sqlQuery SQL語句(可選)
* @todo 完成其他DB對象
*/
function ShowPage($DB, $table, $limit, $sqlQuery = "") {
// $DB is a object contain some method access to mySql
// $table is the table name you want to access (if you want to access only some part of the table you should use the fourth parameter instead)
// $limit is the page limit value
// $sqlQuery must be a SQL Query (E.G. SELECT * FROM table WHERE id like '100';)
$this->DB = $DB;
$this->table = $table;
$this->limit = $limit;
$this->sqlQuery = $sqlQuery;
$tmpGET = $_GET;
unset($tmpGET[$this->pVar]);
$this->setVar($tmpGET);
if ($sqlQuery == "") {
$tmpQuery = "SELECT * FROM $this->table";
}
else {
$tmpQuery = $sqlQuery;
}
//adodb hack start
$rs =& $DB->Execute($tmpQuery) or die($DB->ErrorMsg());
//$tPage =$this->total= $DB->Affected_Rows( );
$tPage = $rs->RecordCount();
$this->tRow = $tPage;
//adodb hack end
$this->tPage = ceil($tPage / $this->limit); // caculate the total page
if ($this->tPage > 0) {
// build an array which contain all page number (E.G. array(1, 2, 3, 4))
for ($i = 1; $i < $this->tPage + 1; $i++) {
$this->tPageArray[] = $i;
}
}
else {
$this->tPageArray[] = 0;
}
}
/**
* 設置翻頁參數
* @param string $pvar 翻頁參數
*/
function SetPageVar($pvar = 'p') {
// set type turnpage parameter (E.G. http://www.aa.com/index.php?p=1)
$this->pVar = $pvar;
}
/**
* 返回總行數
* @return int
*/
function GetTotalRow() {
return $this->tRow;
}
/**
* 返回翻頁參數
* @return string
*/
function GetPageVar() {
return $this->pVar;
}
/**
* 返回當前頁數
* @return int
*/
function GetCurrentPage() {
return $_GET[$this->pVar];
}
/**
* 返回總頁數數組
* @return array
*/
function GetTotalPageArray() {
return $this->tPageArray;
}
/**
* 返回總頁數
* @return int
*/
function GetTotalPage() {
return $this->tPage;
}
/**
* 返回每頁顯示條數
* @return int
*/
function GetLimit() {
return $this->limit;
}
/**
* 返回數據行數
* @return int
*/
function GetRowFrom() {
if ($this->GetCurrentPage() <= 1) {
return 0;
}
else {
return ($this->GetCurrentPage() - 1) * $this->GetLimit();
}
}
/**
* 返回朝左翻
* @return string
*/
function GetLeftSide() {
$current = $_GET[$this->pVar];
if (!$current) {
$current = 1;
}
if ($current<1) {
$current = 1;
}
if ($current>$this->tPage) {
$current = $this->tPage;
}
if ($this->tPage > 1) {
if ($current > 10) {
$this->outputLeftSide = '<a href='.$this->file.'?'.$this->pVar.'='.($current-10).($this->varstr).' title="revious 10 Pages"><<<</a> ';
}
if ($current > 1) {
$this->outputLeftSide .= '<a href='.$this->file.'?'.$this->pVar.'='.($current-1).($this->varstr).' title="revious Page"><<</a> ';
}
}
return str_replace('"', "'", $this->outputLeftSide);
}
/**
* 返回朝右翻
* @return string
*/
function GetRightSide() {
$current = $_GET[$this->pVar];
if (!$current) {
$current = 1;
}
if ($current>$this->tPage) {
$current = $this->tPage;
}
if ($current<1) {
$current = 1;
}
if ($this->tPage > 1) {
if ($current<$this->tPage) {
$this->outputRightSide = '<a href='.$this->file.'?'.$this->pVar.'='.($current+1).($this->varstr).' title="Next Page">>></a> ';
}
if ($this->tPage>10 && ($this->tPage-$current)>=10 ) {
$this->outputRightSide .= '<a href='.$this->file.'?'.$this->pVar.'='.($current+10).($this->varstr).' title="Next 10 Page">>>></a>';
}
}
return str_replace('"', "'", $this->outputRightSide);
}
/**
* 返回一個完整的翻頁選項
* 包含了向左翻(<< <), 直接選擇頁面數字, 向右翻(> >>), 和所需要的Javascript
* @return string
*/
function GetFullSide() {
$sLeftSide = $this->GetLeftSide();
$sRightSide = $this->GetRightSide();
$sCenter = "<select onChange='fnJump(this.value)' name='select'>\n";
$pages = $this->GetTotalPageArray();
foreach($pages as $page) {
$selected = "";
if($page == $this->GetCurrentPage())$selected = "selected";
$sCenter .= " <option value=\"$page\" $selected>$page</option>\n";
}
$sCenter .= "</select>\n";
$sScript = $this->GetTurnPageScript();
return ($sLeftSide ."\n". $sCenter ."\n". $sRightSide ."\n". $sScript);
} // end func
/**
* 返回Sql語句
* @return string
*/
function GetNewSql() {
return $this->sqlQuery . " LIMIT " . $this->GetRowFrom() . ", " . $this->GetLimit();
} // end func
/**
* 返回翻頁時所需的Javascript
* @return string
*/
function GetTurnPageScript() {
return "<SCRIPT LANGUAGE=\"JavaScript\">\n" .
"<!--\n" .
"function fnJump(page)\n" .
"{\n" .
"location.href='".$_SERVER['PHP_SELF']."?" . $this->GetPageVar() ."='+page" . "+'".($this->varstr) . "';\n" .
"}\n" .
"//-->\n" .
"</SCRIPT>\n";
} // end func
/**
* 設置其它參數
* 設置翻頁時需要通過用GET方法一同傳送過去的其他參數
* @param array $data
*/
function setVar($data) {
// set the turnpage addition value need to transfer by get mode
foreach ($data as $k=>$v) {
$this->varstr.='&'.$k.'='.urldecode($v);
}
}
}
?>
[/PHP] |
|