search
HomeBackend DevelopmentPHP Tutorial一个可查询所有表的“通用”查询分页类_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.="";
      while($found=@mysql_fetch_array($sult)){
         $echo_content.="";
         //显示用户指定的字段,此处需仔细看
         for($i=2;$i            $echo_content.="";
         }
         $echo_content.="";
      }

      //分页
      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.="";
      }else{
         $echo_content.="&下页";
      }
      $echo_content.="
";
         $echo_content.="".$found[1]."
".$found[$i]."
上页&";
      }else{
         $echo_content.="
上页&";
      }
      //显示页码
      for($i=1;$i         if ($_GET[offset]/$this->c_rows==($i-1)){
            $echo_content.="&第".$i."页&";
         }else{
            $echo_content.="&c_rows.">".$i."&";
         }
      }
      //显示下一页
      if ($nex_page!=0 and ($_GET[offset]+$this->c_rows)total){
         $echo_content.="&下页
";
      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()是将查询结果集存在一个二维数组里,本例中没有用到。
*/
?>
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
How to calculate the total number of elements in a PHP multidimensional array?How to calculate the total number of elements in a PHP multidimensional array?May 15, 2025 pm 09:00 PM

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

What are the characteristics of do-while loops in PHP?What are the characteristics of do-while loops in PHP?May 15, 2025 pm 08:57 PM

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

How to hash strings in PHP?How to hash strings in PHP?May 15, 2025 pm 08:54 PM

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

How to implement array sliding window in PHP?How to implement array sliding window in PHP?May 15, 2025 pm 08:51 PM

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

How to use the __clone method in PHP?How to use the __clone method in PHP?May 15, 2025 pm 08:48 PM

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

How to use goto statements in PHP?How to use goto statements in PHP?May 15, 2025 pm 08:45 PM

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

How to implement data statistics in PHP?How to implement data statistics in PHP?May 15, 2025 pm 08:42 PM

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

How to use anonymous functions in PHP?How to use anonymous functions in PHP?May 15, 2025 pm 08:39 PM

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values ​​of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment