<?
/*
程序名:php分页显示ver1.1
作者:周玉增
修改时间:2002.3.28
注:此程序在sharetop的分页程序基础上完成
  mysql_select_db($dbname,$conn);        //选择数据库
  $rs1=new TViewPage;     //生明$rs1为TViewPage类
  $MaxLine=3;               //每页显示记录数
  if(empty($page)&&empty($offset))               //初始化偏移量
      $page=1;
   if(!empty($page))
    $offset=($page-1)*$MaxLine;
   $offset=ceil($offset);   $rs1->TViewPageSet("Userinfo",$MaxLine,$offset); //初始化类的一些基本参数第一个参数为表名,第二个参数为每页显示记录数,第三个为偏移量。
   $rs1->SetCondition($sql);     //初始化sql条件
   $rs=$rs1->ReadList();           //读取记录内容   <?$rs1->ThePage();$rs1->Page();?>//显示分页
   for($i=0;$i<count($rs);$i++) {     //循环显示内容
       echo $rs[$i][id];
   }
*/
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 TViewPageSet($TB,$ML,$OF) {
$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; $tmp=$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=ceil($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]."=".$this->PageQuery[$i];
}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>";
echo "<form name=formgo action=# method=post>到<input type=textbox name=page size=3><input name=gogogo type=submit value=go>页</form>";
}
//******end class
}
?>