2014-03-05 41 views
0

賦值我有一個MySQL SELECT查詢返回如下:使用返回的行作爲PHP變量名,並從CSV

$getDbVars = "SELECT `var_code` FROM `campaign_variables`"; 

$name 
$videoURL 
$leadLogo 
$brand 
$competitor 

我也有一個CSV,它的列是與上面相同。當然,CSV每列保存數百行。

andres,http://somewhere.com/vid1.mp4,http://somewhere.com/logo1.png,brand1,andres-competitor 
allan,http://somewhere.com/vid5.mp4,http://somewhere.com/logo5.png,brand5,allan-competitor 
alexander,http://somewhere.com/vid7.mp4,http://somewhere.com/logo7.png,alexander-competitor 

首先,我需要使用任何sql查詢返回作爲變量名稱。

然後,我需要能夠循環訪問CSV,並且每次都將值分配給這些變量。因此,例如,在循環的第一次掃描:

$name = 'andres'; 
$videoURL = 'http://somewhere.com/vid1.mp4'; 
$leadLogo = 'http://somewhere.com/logo1.png'; 
$brand = 'brand1'; 
$competitor = 'andres-competitor'; 

等,爲CSV的每一行。

請讓我知道,如果上述沒有多大意義。我必須這樣做的原因是因爲有些情況下,同一個查詢將返回不同數量的行。因此,會有不同的「變量名稱」。好的是,CSV將始終具有與查詢返回的行數相同的列數。

+0

請詳細說明。顯示一些示例csv-rows和選擇查詢以及結果應該是什麼樣子。 –

+1

'list($ name,$ videoURL,...)= mysql_fetch_assoc($ result)',基本上。你想要的是基本的PHP和數據庫操作。你可能只是在反思一切。 –

+0

@TobiasKun謝謝,我添加了更多細節 – jeffimperial

回答

0

我認爲最好的方式實現這一目標是數組:

$keys = array("name", "videoURL", "leadLogo", "brand", "competitor"); 
$csv = $your_csv_as_array_data; 

$data = array(); 
foreach($csv as $row) { 
    $temp = array(); 
    for($i = 0; $i < count($keys); $i++) { 
     $temp[$keys[$i]] = $row[$i]; 
    } 
    $data[] = $temp; 
} 

這會給你下面的數組:

array (
    [0] => array(
     "name" => "...", 
     "videoURL" => "...", 
     "leadLogo" => "...", 
     "brand" => "...", 
     "competitor" => "..." 
    ), 
    [1] => array(
     "name" => "...", 
     "videoURL" => "...", 
     "leadLogo" => "...", 
     "brand" => "...", 
     "competitor" => "..." 
    ), 
    [2] => array(
     "name" => "...", 
     "videoURL" => "...", 
     "leadLogo" => "...", 
     "brand" => "...", 
     "competitor" => "..." 
    ) 
    . 
    . 
    . 
) 
相關問題