用例:
require("inc/conn.inc"); //调用数据库连接
if !($offset) $offset = 0;
if !($pageline) $pageline = 15; //不要改变这两个变量的名称;
$classtest = new TViewPage("tablename",$pageline,$offset);
$recordSet = $classtest->getRecords();
$classtest->showFullFunc();
$k=count($recordSet);
for($i=0;$i<$k;$i++)
echo $recordSet[$i]["Field_Name"];
......
***********************************************/
class TViewPage {
var $Table; //表名
var $MaxLine=15; //每页显示行数
var $Offset; //记录偏移量
var $Total; //记录总数
var $Number; //本页读取的记录数
var $Result; //读出的结果
var $TPages; //总页数
var $CPages; //当前页数
var $Condition; //数据库搜索条件
var $PageQuery; //分页显示要传递的参数
var $strPageFrist = '首页'; //设置导航条翻页提示符
var $strPagePrev = '上页';
var $strPageNext = '下页';
var $strPageLast = '末页';
var $strDispPageFrist; //设置导航条翻页(未激活状态)提示符
var $strDispPagePrev; //如果不设置,导航条将不区别显示激活与否
var $strDispPageNext; //本项配合图形可获得最佳效果
var $strDispPageLast;
//******构造函数*************
//参数:表名、最大行数、偏移量
function TViewPage($TB,$ML,$OF){
$this->Table = $TB;
$this->MaxLine = $ML;
$this->Offset = (floor($OF/$ML)) * $ML;
}
//********设置显示条件*********
//如:where id="$id" order by id desc
//要求是字串,符合SQL语法(本字串将加在SQL语句后)
function setCondition($s){
$this->Condition=$s;
}
//******设置传递参数************
// key参数名 value参数值
// 如:setpagequery("id",$id);如有多个参数要传递,可多次调用本函数
function setPageQuery($key,$value){
$tmp[key]=$key;$tmp[value]=$value;
$this->PageQuery[]=$tmp;
}
//********设置导航条分页显示的字符或图形*********
//如:首页 或 <IMG SRC=frist.gif align=absmiddle> 等。
//要求是字串,符合HTML语法显示的要求
//如果不设置,导航条使用默认字符
function setPageFrist($s,$sd=""){
$this->strPageFrist=$s;
$this->strDispPageFrist=$sd;
}
function setPagePrev($s,$sd=""){
$this->strPagePrev=$s;
$this->strDispPagePrev=$sd;
}
function setPageNext($s,$sd=""){
$this->strPageNext=$s;
$this->strDispPageNext=$sd;
}
function setPageLast($s,$sd=""){
$this->strPageLast=$s;
$this->strDispPageNext=$sd;
}
//********读取记录***************
// 主要工作函数,根据所给的条件从表中读取相应的记录
// 返回值是一个二维数组,Result[记录号][字段名]
function getRecords() {
$SQL="SELECT Count(*) AS total FROM ".$this->Table." ".$this->Condition;
$result=mysql_query($SQL) or die(mysql_error());
$row=mysql_fetch_Array($result);
$this->Total=$row[total];
$this->TPages=ceil($this->Total/$this->MaxLine);
$this->CPages=$this->Offset/$this->MaxLine+1;
if($this->Total>0) {
$SQL="SELECT * FROM ".$this->Table." ".$this->Condition.
" LIMIT ".$this->Offset." , ".$this->MaxLine;
$result=mysql_query($SQL) or die(mysql_error());
$this->Number=mysql_num_rows($result);
while($row=mysql_fetch_Array($result)) $this->Result[]=$row;
}
return $this->Result;
}
//**********显示页码、页数*************
//显示当前页及总页数,参数s指定不同的显示风格,可以不指定
function getPages($s=0) {
switch($s){
case 90:
return $this->CPages;
break;
case 91:
return $this->TPages;
break;
default:
return "第".$this->CPages."页/共".$this->TPages."页";
}
}
//**********显示总记录数量************
//显示当前页及总页数
function getTotal() {
return $this->Total;
}
//**********接受用户输入每页最多显示记录数************
//$Type=1为下拉单输入,可让用户在iMin~iMax之间选择。
//如果单独使用此类方法(函数),请在调用此类方法时配套加入
//<form>元素及提交按钮,Action可以用$PHP_SELF值。
function inputPageLines($Type=0,$iMin=1,$iMax=30) {
if ($Type == 1){
echo '<select name=pageline>';
for($i=$iMin ; $i <= $iMax ; $i++)
if ($i == $this->MaxLine) echo '<option value='.$i.' selected>'.$i.'</option>';
else echo '<option value='.$i.'>'.$i.'</option>';
echo '</select>';
}else echo '<input type=text size=2 maxlength=3 name=pageline value='.$this->MaxLine.'>';
}
//**********接受用户选择页码************
//如果单独使用此类方法(函数),请在调用此类方法时配套加入
//<form>元素及提交按钮,Action可以用$PHP_SELF值。
function selectPage() {
if ($this->TPages > 1) {
echo '<select size=1 name=offset>';
for($i=0 ; $i < $this->TPages ; $i++)
if ($this->Offset == ($i) * $this->MaxLine)
echo '<option value='.($i * $this->MaxLine).' selected>第'.($i+1).'页</option>';
else
echo '<option value='.($i * $this->MaxLine).'>第'.($i+1).'页</option>';
echo '</select>';
$k=count($this->PageQuery);
for($i=0;$i<$k;$i++){
echo '<input type=hidden name='.$this->PageQuery[$i][key].' Value='.$this->PageQuery[$i][value].'>';
}
}
}
//**********显示导航按钮或文字*************
//显示首页、下页、上页、未页,并加上要传递的参数
function showNavigator() {
$outstr='';
$first=0;
$next=$this->Offset+$this->MaxLine;
$prev=$this->Offset-$this->MaxLine;
$last=($this->TPages-1)*$this->MaxLine;
$k=count($this->PageQuery);
$strQuery="";//生成一个要传递参数字串
for($i=0;$i<$k;$i++){
$strQuery.="&".$this->PageQuery[$i][key]."=".$this->PageQuery[$i][value];
}
if($this->Offset>=$this->MaxLine)
$outstr.="<A href=$PHP_SELF?offset=".$first."&pageline=".$this->MaxLine.$strQuery.">".$this->strPageFrist."</A>|";
else
$outstr.=$this->strPageFrist."|";
if($prev>=0)
$outstr.="<A href=$PHP_SELF?offset=".$prev."&pageline=".$this->MaxLine.$strQuery.">".$this->strPagePrev."</A>|";
else
$outstr.=$this->strPagePrev."|";
if($next<$this->Total)
$outstr.="<A href=$PHP_SELF?offset=".$next."&pageline=".$this->MaxLine.$strQuery.">".$this->strPageNext."</A>|";
else
$outstr.=$this->strPageNext."|";
if($this->TPages!=0 && $this->CPages<$this->TPages)
$outstr.="<A href=$PHP_SELF?offset=".$last."&pageline=".$this->MaxLine.$strQuery.">".$this->strPageLast."</A>";
else
$outstr.=$this->strPageLast;
return $outstr;
}
//**********分页显示的全部功能*************
//提供一个整合的解决方案,没有特殊要求的话直接用它就行了。
function showFullFunc($barBgColor="") {
echo '<table width=100% cellspacing=0 cellpadding=0 border=0>';
echo '<form Action="'.$PHP_SELF.'" method="POST">';
echo '<tr bgcolor="'.$barBgColor.'">';
echo '<td align=left> ';
echo $this->getPages().' ';
echo $this->showNavigator().' ';
echo '共 '.$this->getTotal().' 行';
echo '</td>';
echo '<td width=45% align=right>';
echo '每页';
$this->inputPageLines(1);
echo '行';
if ($this->TPages > 1) echo ' / 直接跳转到';
$this->selectPage();
echo ' <input type=Submit value="Reset">';
echo ' </td>';
echo '</tr></form></table>';
}
//******end class
}
?> |