【参加讨论】 <?php
/*********************************************
tviewpage v 1.0 分页显示mysql数据库记录的类
作者:sharetop
e-mail:ycshowtop@21cn.com
时间:2000-8-31
本类没有提供连接数据库的功能,所以需在外部打开相应的数据库。
本类也没有提供显示记录的功能,只是分页读取记录至 result二维数组中。
需在外部自定义数据显示格式。
***********************************************/
class tviewpage {
var $table; //表名
var $maxline; //每页显示行数
var $offset; //记录偏移量
var $total; //记录总数
var $number; //本页读取的记录数
var $result; //读出的结果
var $tpages; //总页数
var $cpages; //当前页数
var $condition; //显示条件 如:where id='$id' order by id desc
var $pagequery; //分页显示要传递的参数
//******构造函数*************
//参数:表名、最大行数、偏移量
function tviewpage($tb,$ml,$of=0){
$this->table=$tb;
$this->maxline=$ml;
$this->offset=$of;
$this->condition="";
}
//********设置显示条件*********
//如: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;
}
//********读取记录***************
// 主要工作函数,根据所给的条件从表中读取相应的记录
// 返回值是一个二维数组,result[记录号][字段名]
function readlist() {
$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];
if($this->total>0) { //根据条件 condition
$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;
}
//**********显示页数*************
//显示当前页及总页数
function thepage() {
$this->tpages=ceil($this->total/$this->maxline);
$this->cpages=$this->offset/$this->maxline+1;
echo "第".$this->cpages."页/共".$this->tpages."页";
}
//**********显示翻页按钮*************
//此函数要在thepage()函数之后调用!!!
//显示首页、下页、上页、未页,并加上要传递的参数
function page() {
$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)
echo "<a href=$php_self?offset=".$first.$strquery.">首页</a>|";
if($prev>=0)
echo "<a href=$php_self?offset=".$prev.$strquery.">上一页</a>|";
if($next<$this->total)
echo "<a href=$php_self?offset=".$next.$strquery.">下一页</a>|";
if($this->tpages!=0 && $this->cpages<$this->tpages)
echo "<a href=$php_self?offset=".$last.$strquery.">末页</a>";
}
//******end class
}
?>