2014-02-26 25 views
1

我遇到問題,想知道是否有人可以幫我解決問題。我會盡可能清楚地解釋我的問題。我正在嘗試製作數據Feed,以便我可以自動將我的廣告資源實施到其他網站。我已完成數據饋送,但我遇到的問題是爲每輛車添加所有車輛圖像。我只能得到數據饋送中每輛車的主要圖像。如何停止重複數據庫查詢結果的輸出PHP

這是怎麼我的數據庫表設置

categories - cat_id, cat_name 

pictures - key_id, veh_id, pic_name 

vehicles - veh_id, cat_id, _make, _model, sub_model etc..... 

下面是我datafeed.php代碼

我希望能夠以顯示所有在圖像表圖片在一條線上的每個項目。我試圖JOIN車輛圖片但不喜歡輸出。它在數據饋送上多次列出每輛汽車。如果一輛車有12張照片,它會在數據饋送上列出該單車12次。任何幫助將不勝感激。

$fh = fopen($myFile, 'w'); 

$query = "SELECT * FROM vehicles WHERE _sold = 'STOCKED' ORDER BY _make ASC";        
$result = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 

    $current_date = date('Y-m-d'); 
    $due_date = date("Y-m-d", strtotime(date("Y-m-d", strtotime(date("Y-m-d"))) . "+2 week")); 

    $stringData = "|{$row['_make]}|{$row['_model']}| |{$row['_body']} |Damaged|{$row['_year']}| |{$row['_price']} ||{$row['_desc']} ||{$row['_title']} |{$row['_miles']}| |{$row['veh_id']}|$due_date|http://www.mywebsite.com/{$row['_year']}/{$row['_make']}/{$row['veh_id']}|http://www. mywebsite.com/{$row['veh_id']}{$row['_thumbnail']}| | | | | | | | | |Buy it Now \n"; 

    fwrite($fh, $stringData); 

} ; 

fclose($fh); 
include 'config/close.php'; 
+0

你用$ stringData做什麼? – giuseppe

+1

嘗試在您的車輛和圖片JOIN查詢中添加:GROUP BY veh_id。 – Grant

+0

嘗試GROUP_CONCAT('pic_id')與GROUP BY'veh_id',加入車輛和圖片表 – avisheks

回答

1

嘗試使用下面的SQL:

SELECT C.cat_name, V. * , pictures 
FROM vehicles AS V 
LEFT JOIN categories AS C ON C.cat_id = V.cat_id 
LEFT JOIN (
     SELECT P.veh_id, GROUP_CONCAT(pic_name) AS pictures 
     FROM pictures AS P 
     LEFT JOIN vehicles AS V ON P.veh_id = V.veh_id 
     GROUP BY P.veh_id 
    ) AS P ON P.veh_id = V.veh_id 

上面的查詢將返回所有車輛帶或不帶圖像。你可以簡單地explodepictures場逗號來獲取所有圖片

見工作演示here

+0

謝謝你,工作得很好。我在一個數組中輸入圖片,然後使用foreach來爆炸它們。 – user3354780

0

這是我datafeed.php腳本現在的樣子。非常感謝您的幫助,非常感謝。

<?php 
include 'config/config.php'; 
include 'config/opendb.php'; 

$myFile = "../datafeed.txt"; 

if(!file_exists("$myFile")) 
{ 
die("File not found"); 
} 

$fh = fopen($myFile, 'w'); 

$query = "SELECT C.cat_name, V. * , pictures 
      FROM vehicles AS V 
      LEFT JOIN categories AS C ON C.cat_id = V.cat_id 
      LEFT JOIN ( 
       SELECT P.veh_id, GROUP_CONCAT(pic_name) AS pictures 
       FROM pictures AS P 
       LEFT JOIN vehicles AS V ON P.veh_id = V.veh_id 
       GROUP BY P.veh_id 
       ) AS P ON P.veh_id = V.veh_id"; 

$result = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 

$picname[] = $row['pictures']; 

foreach($picname as $pic) 
{ 
    $carPics = explode(',',$pics); 
} 

$current_date = date('Y-m-d'); 
$due_date = date("Y-m-d", strtotime(date("Y-m-d", strtotime(date("Y-m-d"))) . "+2 week")); 

$stringData = "|{$row['_make]}|{$row['_model']}| |{$row['_body']} |Damaged|{$row['_year']}| |{$row['_price']} ||{$row['_desc']} ||{$row['_title']} |{$row['_miles']}| |{$row['veh_id']}|$due_date|http://www.mywebsite.com/{$row['_year']}/{$row['_make']}/{$row['veh_id']}|http://www. mywebsite.com/$carPic[0]|http://www. mywebsite.com/$carPic[1]|http://www. mywebsite.com/$carPic[2]|http://www. mywebsite.com/$carPic[3]|http://www. mywebsite.com/$carPic[4]|http://www. mywebsite.com/$carPic[5]|http://www. mywebsite.com/$carPic[6]|http://www. mywebsite.com/$carPic[7]|http://www. mywebsite.com/$carPic[8]|http://www. mywebsite.com/$carPic[9]|Buy it Now \n"; 

fwrite($fh, $stringData); 

} ; 

fclose($fh); 
include 'config/close.php';