一个可查询所有表的“通用”查询分页类
时间:2007-04-29 来源:windlike
为了学习php类,找了几个Mysql分页查询类,感觉这个是最容易理解的。
?php
/*****************************************************
作者: tonera
现在的类应该是这样的:
只传入一个查询语句,显示行数,偏移量,连接句柄就显示出结果
*****************************************************/
class browser{
var $c_rows; //要显示的行数
var $c_fields; //要显示的字段
var $c_result; //查询的数据连接句柄
var $c_query; //最终构造的查询语句
var $c_found; //结果集句柄
var $c_error; //错误收集器
var $c_offset; //分页显示的偏移量
var $c_totalpage; //总页数
var $c_nowpage; //当前页数
var $total; //结果集的总数
//初始化
function initialize($sql,$rows,$offset,$connect){
//查询的连接句柄和要显示的行数
$this->c_rows=$rows;
$this->c_result=$connect;
//偏移量
if (empty($offset)){
$offset=0;
}
$this->c_offset=$offset;
//构造查询
$this->c_query=$sql." limit ".$offset.",".$rows;
//得到结果总数
$result=@mysql_query($sql,$connect);
//错误中断
if ($result==false){
$this->c_error.="查询错误,请检查输入的sql语句是否正确。
";
exit;
}
$this->total=@mysql_num_rows($result);
//计算总页数
$this->c_totalpage=ceil($this->total/$rows);
//计算当前页码
$this->c_nowpage=ceil(($offset+$rows)/$rows);
}
//得到一个结果集句柄
function GetFound(){
$this->c_found=@mysql_query($this->c_query,$this->c_result);
//错误中断
if ($this->c_found==false){
$this->c_error.="查询错误,请检查输入的sql语句是否正确。
";
exit;
}
}
//翻页链接(风格可以自定)
function ActionPage(){
$string="";
$pre_page=$this->c_offset-$this->c_rows;
$nex_page=$this->c_offset+$this->c_rows;
if($pre_page0){
$string.="上一页";
}else{
$string.="上一页";
}
if ($nex_page$this->total && $this->total!=0){
$string.="|下一页";
}else{
$string.="|下一页";
}
$string.="&&&&当前页码:".$this->c_nowpage."/".$this->c_totalpage;
return $string;
}
//显示字段分析器
//解析$this->c_query,得到要显示出来的字段值
function fieldvar(){
$tempvar=explode(" ",$this->c_query);
$this->c_fields=explode(",",$tempvar[1]); //字段值(数组)
}
//显示样式1
function ShowTable(){
$this->fieldvar(); //取得要显示的字段
$this->GetFound(); //得到结果集句柄
//显示数据到一个表
$echo_content.="";
while($found=@mysql_fetch_array($this->c_found)){
$echo_content.="";
$echo_content.=".$found[0].">".$found[1]."";
//显示用户指定的字段,此处需仔细看
for($i=2;$icount($this->c_fields);$i++){
$echo_content.="".$found[$i]."";
}
$echo_content.="";
}
$echo_content.="";
return $echo_content;
}
}
//END
///*例子
//include 'connect.inc.php';
// connect db
$hostname="localhost";
$username="test";
$password="123456";
$dbname="test";
$connection = mysql_connect($hostname,$username,$password);
mysql_select_db($dbname);
$gggg=new browser();
//$sql="select auto_id,news_title from news order by newstime desc";
$sql="select distinct id,name from student";
$gggg->initialize($sql,4,$_GET[offset],$connection); //初始化
$tempvar=$gggg->ActionPage(); //显示分页符
echo $tempvar."
";
$gggg->fieldvar(); //得到显示字段
$resultt=$gggg->GetFound(); //得到结果集句柄
//自定义显示效果
$result=$gggg->c_found;
while($rows=mysql_fetch_array($result)){
echo $rows[0]."||".$rows[1]."
";
}
//通过类中的显示风格来显示
$tee=$gggg->ShowTable();
echo $tee;
//*/
?>;
?php
/*****************************************************
作者: tonera
现在的类应该是这样的:
只传入一个查询语句,显示行数,偏移量,连接句柄就显示出结果
*****************************************************/
class browser{
var $c_rows; //要显示的行数
var $c_fields; //要显示的字段
var $c_result; //查询的数据连接句柄
var $c_query; //最终构造的查询语句
var $c_found; //结果集句柄
var $c_error; //错误收集器
var $c_offset; //分页显示的偏移量
var $c_totalpage; //总页数
var $c_nowpage; //当前页数
var $total; //结果集的总数
//初始化
function initialize($sql,$rows,$offset,$connect){
//查询的连接句柄和要显示的行数
$this->c_rows=$rows;
$this->c_result=$connect;
//偏移量
if (empty($offset)){
$offset=0;
}
$this->c_offset=$offset;
//构造查询
$this->c_query=$sql." limit ".$offset.",".$rows;
//得到结果总数
$result=@mysql_query($sql,$connect);
//错误中断
if ($result==false){
$this->c_error.="查询错误,请检查输入的sql语句是否正确。
";
exit;
}
$this->total=@mysql_num_rows($result);
//计算总页数
$this->c_totalpage=ceil($this->total/$rows);
//计算当前页码
$this->c_nowpage=ceil(($offset+$rows)/$rows);
}
//得到一个结果集句柄
function GetFound(){
$this->c_found=@mysql_query($this->c_query,$this->c_result);
//错误中断
if ($this->c_found==false){
$this->c_error.="查询错误,请检查输入的sql语句是否正确。
";
exit;
}
}
//翻页链接(风格可以自定)
function ActionPage(){
$string="";
$pre_page=$this->c_offset-$this->c_rows;
$nex_page=$this->c_offset+$this->c_rows;
if($pre_page0){
$string.="上一页";
}else{
$string.="上一页";
}
if ($nex_page$this->total && $this->total!=0){
$string.="|下一页";
}else{
$string.="|下一页";
}
$string.="&&&&当前页码:".$this->c_nowpage."/".$this->c_totalpage;
return $string;
}
//显示字段分析器
//解析$this->c_query,得到要显示出来的字段值
function fieldvar(){
$tempvar=explode(" ",$this->c_query);
$this->c_fields=explode(",",$tempvar[1]); //字段值(数组)
}
//显示样式1
function ShowTable(){
$this->fieldvar(); //取得要显示的字段
$this->GetFound(); //得到结果集句柄
//显示数据到一个表
$echo_content.="";
while($found=@mysql_fetch_array($this->c_found)){
$echo_content.="";
$echo_content.=".$found[0].">".$found[1]."";
//显示用户指定的字段,此处需仔细看
for($i=2;$icount($this->c_fields);$i++){
$echo_content.="".$found[$i]."";
}
$echo_content.="";
}
$echo_content.="";
return $echo_content;
}
}
//END
///*例子
//include 'connect.inc.php';
// connect db
$hostname="localhost";
$username="test";
$password="123456";
$dbname="test";
$connection = mysql_connect($hostname,$username,$password);
mysql_select_db($dbname);
$gggg=new browser();
//$sql="select auto_id,news_title from news order by newstime desc";
$sql="select distinct id,name from student";
$gggg->initialize($sql,4,$_GET[offset],$connection); //初始化
$tempvar=$gggg->ActionPage(); //显示分页符
echo $tempvar."
";
$gggg->fieldvar(); //得到显示字段
$resultt=$gggg->GetFound(); //得到结果集句柄
//自定义显示效果
$result=$gggg->c_found;
while($rows=mysql_fetch_array($result)){
echo $rows[0]."||".$rows[1]."
";
}
//通过类中的显示风格来显示
$tee=$gggg->ShowTable();
echo $tee;
//*/
?>;
相关阅读 更多 +
排行榜 更多 +