2014-10-09 57 views
0

這裏我試圖從四個表中獲取數據。我成功地獲得了結果。但是,我需要獲取過濾結果的表名。例如,我得到了10個結果。來自TBL_CAR的3個結果,來自TBL_BIKE的3個結果,來自TBL_TRUCK的2個結果,來自TBL_BUS的2個結果。我想將這些不同的鏈接應用於這些結果。獲取數據庫表名作爲列名

如何將數據庫表名稱作爲列名將不同的url鏈接應用於這些結果?

$sql = "(SELECT model_name, maker_url, model_url FROM ".TBL_CAR_ADD_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3) 
    UNION 
    (SELECT model_name, maker_url, model_url FROM ".TBL_BIKE_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3) 
    UNION 
    (SELECT model_name, maker_url, model_url FROM ".TBL_TRUCK_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3) 
    UNION 
    (SELECT model_name, maker_url, model_url FROM ".TBL_BUS_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)"; 
    $res = mysql_query($sql, $CN); 
    $rows = array(); 
    while($row = mysql_fetch_array($res)) 
    { 
     if(resutls FROM TBL_CAR) 
     { 
     $rows[] = array('url' => asort_get_url(CAR_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
     } 
     elseif(results FROM TBL_BIKE) 
     { 
      $rows[] = array('url' => asort_get_url(BIKE_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bike_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
     } 
     elseif(results FROM TBL_TRUCK) 
     { 
      $rows[] = array('url' => asort_get_url(TRUCK_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_truck_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
     } 
     elseif(resutls FROM TBL_BUS) 
     { 
      $rows[] = array('url' => asort_get_url(BUS_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bus_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
     } 
    } 
    echo stripslashes(json_encode($rows,JSON_PRETTY_PRINT)); 
+0

此代碼是有點可怕,我會鼓勵你考慮遷移到[PDO](HTTP:// PHP .net/PDO_mysql)而不是mysql_ *;儘管讓代碼自動更好,但這不是一個神奇的項目,但我認爲如果您開始閱讀使用PDO的代碼,並比較它如何執行操作,可能會幫助您找到更好的方法來構建結構。 [Here's](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)一個快速教程,幫助你開始。 – TML 2014-10-09 04:44:28

回答

3

一個簡單的方法是將表名添加爲常量到您的查詢,如:

SELECT 'TBL_CAR_ADD_MODELS' table_name, model_name, maker_url, model_url FROM ".TBL_CAR_ADD_MODELS." WHERE model_status = '1' AND  model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3) 
UNION 
(SELECT 'TBL_BIKE_MODELS',model_name, maker_url, model_url FROM ".TBL_BIKE_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3) 
UNION 
(SELECT 'TBL_TRUCK_MODELS', model_name, maker_url, model_url FROM ".TBL_TRUCK_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3) 
UNION 
(SELECT 'TBL_BUS_MODELS', model_name, maker_url, model_url FROM ".TBL_BUS_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3) 

這樣你只是檢查table_name的列,以確定它來自哪個表。

+0

除了@Trent的建議之外,您還可以將字符串中的常量權限降低爲: '「(SELECT model_name,maker_url,model_url,'」.TBL_CAR_ADD_MODELS「'as tbl_name FROM」.TBL_CAR_ADD_MODELS。「..' – TML 2014-10-09 04:42:00

+0

@TML它不會返回表名作爲列名 – Karuppiah 2014-10-09 04:58:12

+1

'「...'」.TBL_CAR_ADD_MODELS。「'...」'應該給你常量TBL_CAR_ADD_MODELS的值,用單引號括起來;將「as tbl_name「後它會導致你的數據庫返回一個名爲」tbl_name「的僞列,其值是被插入(正確引用)到字符串中的字符串常量。如果你正在努力,可能會更新問題,所以我們可以看到你到目前爲止已經嘗試過的東西 – TML 2014-10-09 05:01:07

0

只需使用列名稱作爲靜態列值;

(SELECT model_name, maker_url, model_url, ".TBL_CAR_ADD_MODELS." AS table_name 
    FROM ".TBL_CAR_ADD_MODELS." WHERE model_status = '1' ..... 
... 
if ($rows[3] eq TBL_CAR_ADD_MODELS) { ... } 
... 
+0

這些引用會讓他感到不舒服,可能是一個好主意,要麼更仔細地匹配他的代碼風格,要麼提供更全面的重寫他可以在更大的範圍內看到它。 – TML 2014-10-09 04:45:27

+0

它不返回表名作爲列名。 「字段列表」中的未知列'fa_car_add_models' – Karuppiah 2014-10-09 04:53:10

+0

是的,因爲他忘記告訴你如何正確引用它;這正是我對他的評論意味着要發出的。 – TML 2014-10-09 04:59:08

1

另一種方法可能會尋找到你的數據庫中創建一個VIEW,也許是這樣的:

CREATE VIEW ITEM_MODELS AS 
    SELECT *, 'cars' as tbl_name TBL_CARS 
    UNION 
    SELECT *, 'bikes' as tbl_name TBL_BIKE 
    UNION 
    SELECT *, 'trucks' as tbl_name TBL_TRUCK 
    UNION 
    SELECT *, 'buses' as TBL_BUS; 

這就大大簡化了你的PHP代碼是這樣的:

$sql = "SELECT model_name, maker_url, model_url, tbl_name FROM ITEM_MODELS WHERE model_status = '1' AND model_url != '${model_url}' AND model_name LIKE '%${q}%' LIMIT 3)"; 
    while($row = mysql_fetch_array($res)) 
    { 
     switch($row['tbl_name']) { 
      case TBL_CAR_ADD_MODELS: 
       $rows[] = array('url' => asort_get_url(CAR_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
       break; 

      case TBL_BIKE_MODELS: 
       $rows[] = array('url' => asort_get_url(BIKE_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bike_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
       break; 

      case TBL_TRUCK_MODELS: 
       $rows[] = array('url' => asort_get_url(TRUCK_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_truck_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
       break; 

      case TBL_BUS_MODELS: 
       $rows[] = array('url' => asort_get_url(BUS_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bus_model_image_path("thumb", $row['maker_url'],$row['model_url'])); 
       break; 

      default: 
       // nothing sensible to do here 
       break; 
     } 
    } 

然後,我們可以繼續簡化代碼,方法是切換這些case語句以進行數組查找,併爲調用corr的*_image_path函數創建一個封裝器ECT fa_FOO_model_image_path基於相同$row['tbl_name']價值,也許最終讓我們喜歡的東西:

$sql = "SELECT model_name, maker_url, model_url, tbl_name FROM ITEM_MODELS WHERE model_status = '1' AND model_url != '${model_url}' AND model_name LIKE '%${q}%' LIMIT 3)"; 
$sort_keys = [ 
    TBL_CAR_ADD_MODELS => CAR_HOMEPAGE, 
    TBL_BIKE_MODELS => BIKE_HOMEPAGE, 
    TBL_TRUCK_MODELS => TRUCK_HOMEPAGE, 
    TBL_BUS_MODELS => BUS_HOMEPAGE 
]; 
    while($row = mysql_fetch_array($res)) 
    { 
     $rows[] = array('url' => asort_get_url($sort_keys[$row['tbl_name']], $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => model_image_path($row['tbl_name'], "thumb", $row['maker_url'],$row['model_url'])); 
    } 
+0

然後,我們可以繼續通過切換這些case語句來設置一個變量,例如'$ sort_key =(CAR_HOMEPAGE | BIKE_HOMEPAGE | TRUCK_HOMEPAGE | BUS_HOMEPAGE);',併爲'* _image_path'函數創建一個類似的包裝基於相同的'$ sort_key'值調用正確的'fa_FOO_model_image_path'。 – TML 2014-10-09 05:14:55