Home  >  Article  >  Database  >  分页显示Mysql数据库记录的类_MySQL

分页显示Mysql数据库记录的类_MySQL

WBOY
WBOYOriginal
2016-06-01 13:56:31760browse

/*********************************************
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 "首页|";
if($prev>=0)
echo "|";
if($this->TPages!=0 && $this->CPages<$this->TPages)
echo "末页";
}

//******end class
}

?>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn