`
scbzdj
  • 浏览: 5618 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
社区版块
存档分类
最新评论

php中一个可查询所有表的“通用”查询分页类

    博客分类:
  • php
阅读更多

一个可查询所有表的“通用”查询分页类 
最近突发奇想,希望写出一个可以针对所有表的查询分页类。因为在实际的开发中,恐怕查询并将结果集分页显示是用得最多的代码,而表的结构是多样的,我想尽可能地提高代码的重用率和维护性。 
以下是我写的,请各位加以指点,测试,看能否进行更好的改进和更多的支持。 
目前还只支持单一的表,不支持联合查询。但未来可以会考虑如何支持。 

代码: 

<?php 
/**************************************************************** 
这个类主要是解决针对很多表的简单数据查询,还在完善中。希望广大网友多提意见和建议。 

我的目的是开发出一个几乎可以适用于所有mysql表的查询并将结果集分页的类。 
可以自动识别查询中要显示的字段值。 

在此,特别声明,要感谢chinaunix的朋友,特别是PHP版的网友们长期以来给我的帮助。 
特别要提的是NightKids,一直以来,他都给了我无私的帮助,甚至是他的源代码。 

这个类可以被任何人自由引用,使用,修改。但请保留这段文字。 
使用这个类造成的一切损失,都与作者tonera无关。 

我还在考虑,对于一些复杂的联合查询,可以派生一个类,重新构造sql实现。 
这个类没有考虑更多的显示风格,你可以自己构造。 
*****************************************************************/ 
class browser{ 
    var $c_table;    //要查询的表名 
    var $c_rows;    //要显示的行数 
    var $c_lation;    //查询的条件 
    var $c_order;    //排序的条件 
    var $c_result;    //查询的数据连接句柄 
    var $c_query;    //最终构造的查询 
    var $c_found;    //结果集 
    var $c_error;    //错误收集器 
    var $c_offset;    //分页显示的偏移量 
    var $total;       //结果集的总数 

    //连接数据库 
    function connect(){ 
       include '../connect.inc.php'; 
       if ($connection==false){ 
          $this->c_error.="没有连接上数据库。
"; 
          exit; 
       } 
       $this->c_result=$connection; 
    } 

    //构造函数,初始化变量 
    function browser($tablename,$row,$sql,$lation,$orderby){ 
       $this->c_table=$tablename; 
       $this->c_rows=$row; 
       if(empty($this->c_offset)){ 
          $this->c_offset=0; 
       } 
       if (empty($tablename) or empty($row) or empty($sql)){ 
          $this->c_error="没有查询的表或没有批定显示多少行或没有查询语句
"; 
       } 
       $this->c_query=$sql; 
       if (!empty($lation)){ 
          $this->c_query.=" ".$lation; 
       } 
       if (!empty($orderby)){ 
          $this->c_query.=" ".$orderby; 
       } 
    } 

    //计算总页数 
    function TatolPage(){ 
       $sult=mysql_query("select count(*) as 'total_rows' from $this->c_table",$this->c_result); 
       if ($sult==false) { 
          $this->c_error.="计算结果集总数目的查询失败,请检查。
"; 
          exit; 
       } 
       $tempvar=mysql_fetch_array($sult); 
       $this->total=$tempvar[0]; 
    } 

    //查询得到结果集,存入数组c_found[][]中 
    function GetFound(){ 
       $sult=mysql_query($this->c_query,$this->c_result) or die(mysql_error()); 
       while ($found=mysql_fetch_array($sult)){ 
          $this->c_found[]=$found; 
       } 
    } 

    //查询数据,并将结果分页存入一个变量 
    function ShowTable(){ 

       $this->connect(); 
       $this->TatolPage(); 

       if (empty($_GET[offset])){ 
          $_GET[offset]=0; 
       } 
       $this->c_query.=" limit ".$_GET[offset].", ".$this->c_rows; 
       $sult=mysql_query($this->c_query,$this->c_result) or die(mysql_error()); 

       //解析query,得到要显示出来的字段值 
       $tempvar=explode(" ",$this->c_query); 
       $fields=explode(",",$tempvar[1]);    //字段值(数组) 

       //显示数据到一个表 
       $echo_content.="<table width=100% border=1>"; 
       while($found=@mysql_fetch_array($sult)){ 
          $echo_content.="<tr><td>"; 
          $echo_content.="<a href=newsshow.php?ID=".$found[0].">".$found[1]."</a></td>"; 
          //显示用户指定的字段,此处需仔细看 
          for($i=2;$i<count($fields);$i++){ 
             $echo_content.="<td>".$found[$i]."</td>"; 
          } 
          $echo_content.="</tr>"; 
       } 

       //分页 
       if ($this->c_rows==0){ 
          $this->c_error.="每页显示的数目不能为0"; 
          exit; 
       } 
       $total_page=ceil($this->total/$this->c_rows); 
       $pre_page=$_GET[offset]-$this->c_rows; 
       //下一页 
       $nex_page=$_GET[offset]+$this->c_rows; 
       //显示上一页 
       if ($pre_page>=0){ 
          $echo_content.="<tr><td colspan=10><a href=$PHP_SELF?offset=".$pre_page.">上页</a>&"; 
       }else{ 
          $echo_content.="<tr><td colspan=10>上页&"; 
       } 
       //显示页码 
       for($i=1;$i<=$total_page;$i++){ 
          if ($_GET[offset]/$this->c_rows==($i-1)){ 
             $echo_content.="&第".$i."页&"; 
          }else{ 
             $echo_content.="&<a href=$PHP_SELF?offset=".($i-1)*$this->c_rows.">".$i."</a>&"; 
          } 
       } 
       //显示下一页 
       if ($nex_page!=0 and ($_GET[offset]+$this->c_rows)<=$this->total){ 
          $echo_content.="&<a href= $PHP_SELF?offset=".$nex_page.">下页</a></td></tr>"; 
       }else{ 
          $echo_content.="&下页</td></tr>"; 
       } 
       $echo_content.="</table>"; 
       return $echo_content; 

    } 
} 

/*例子 
//browser("表名",每页显示的数目,"sql","查询条件","排序条件"); 
$gggg=new browser("news",5,"select auto_id,news_title from news","","order by newstime desc"); 

$temp=$gggg->ShowTable(); 
echo $temp; 

//$gggg->GetFound()是将查询结果集存在一个二维数组里,本例中没有用到。 
*/ 
?>
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics