一個可查詢所有表的“通用”查詢分頁類

來源:互聯網
上載者:User

一個可查詢所有表的“通用”查詢分頁類

最近突發奇想,希望寫出一個可以針對所有表的查詢分頁類。因為在實際的開發中,恐怕查詢並將結果集分頁顯示是用得最多的代碼,而表的結構是多樣的,我想儘可能地提高代碼的重用率和維護性。
以下是我寫的,請各位加以指點,測試,看能否進行更好的改進和更多的支援。
目前還只支援單一的表,不支援聯集查詢。但未來可以會考慮如何支援。

代碼:
<?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.="沒有串連上資料庫。<br>";
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="沒有查詢的表或沒有批定顯示多少行或沒有查詢語句<br>";
}
$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.="計算結果集總數目的查詢失敗,請檢查。<br>";
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()是將查詢結果集存在一個二維數組裡,本例中沒有用到。
*/
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.