2013-07-02 155 views
0

我想輸出我的數據到一個CSV文件,我已經成功完成,但是當你打開它與開放式辦公室這些「發生在字段的開始和結束。 他們怎麼可以去掉?PHP輸出到CSV問題

我的數據開盤前沒有「」。

 
    
    "       Alderwood Nursery  " 
    "Delonix regia - 100ltr 0 
    Delonix regia - 125ltr 3 
    " 
    
,赤楊木幼兒園,所有的綠化苗圃,Andreasens綠色,錦城幼兒園,白菜苗圃,卡斯滕斯花園,E植物努薩,異國情調的苗圃園林綠化,格林斯托克苗圃,肯特赫斯特苗圃,洛根苗圃,植物狂歡,邁爾苗圃,墨菲&諾蘭德苗圃,牛津公園苗圃,太平洋樹Qld有限公司,帕拉拉樹,植物直接昆士蘭

// GET ALL SUPPLIERS 
$suppSQL = "SELECT distinct(s.SupplierName) FROM plant_list_stock ps, item_supplier its , suppliers s where ps.plItemID=its.ItemID and its.SupplierID=s.SupplierID and ps.plID = '$pl_listid' order by s.SupplierName"; 
$supp_sql = mysql_query($suppSQL,$connection) or die("Couldn't get Botanical Name. - " . mysql_error()); 
$suppliers=array(); 
while($row=mysql_fetch_array($supp_sql)) 
{ 
    $suppliers[] = $row['SupplierName']; 
} 



// GET ALL BOTANICAL NAMES 
$botSQL = "SELECT plBotanicalName, plSize FROM plant_list_stock ps WHERE ps.plID = '$pl_listid' order by plBotanicalName"; 
$bot_sql = mysql_query($botSQL,$connection) or die("Couldn't get Botanical Name. - " . mysql_error()); 
$names=array(); 
while($row1=mysql_fetch_array($bot_sql)) 
{ 
    $names[] = $row1['plBotanicalName'] .' - '. $row1['plSize']; 
} 


// GET PLANT STOCK LIST 
$plistSQL = "SELECT plItemID, plBotanicalName, plSize, plQty FROM plant_list_stock WHERE plID = '$pl_listid' AND plContID = '$contid'"; 
$plist_result = mysql_query($plistSQL,$connection) or die("Couldn't get Botanical Name. - " . mysql_error()); 

$nurseries = array(); 
$i=0; 
while ($arrRow = mysql_fetch_assoc($plist_result)) 
{ 
    $stockitemID = $arrRow['plItemID']; 

    $it_supSQL = "SELECT * FROM item_supplier WHERE ItemID = '$stockitemID' ORDER BY ValidFrom Desc"; 
    $it_sup_result = mysql_query($it_supSQL,$connection) or die("Couldn't run Item/Supplier loop. - " . mysql_error()); 

    $it_sup_num = mysql_numrows($it_sup_result); 

    // COMPARE AGAINST ITEM/SUPPLIER TABLE 
    while ($arrRowc = mysql_fetch_array($it_sup_result)) 
    { 
     // GET SUPPLIER DETAILS 
     $supSQL = "SELECT * FROM suppliers WHERE SupplierID = '$arrRowc[SupplierID]'"; 
     $sup_result = mysql_query($supSQL,$connection) or die("Couldn't run Supplier loop. - " . mysql_error()); 

     $sup_num = mysql_numrows($sup_result); 

     $s=0; 
     while ($arrRows = mysql_fetch_array($sup_result)) 
     {   
      $nurseries[][$arrRows['SupplierName']][$arrRowc["BotanicalName"] .' - '. $arrRowc['ProductGroup']] = $arrRowc["Price"]; 
     } 
     $s++; 
    } 
    $i++; 

} 

$price_by_id = array(); 
$botanical_name = array(); 

foreach($nurseries as $keys => $nursery) 
{ 
    foreach($nursery as $n => $plant) 
    { 
     if(in_array($n,$suppliers)) 
     { 
      $supplier_key = array_search($n,$suppliers); 

      foreach($plant as $q => $y) 
      { 
       $botanical_name[$supplier_key][]=$q; 
       $price_by_id[$supplier_key][]=$y; 
      } 
     } 
    } 
} 

$nursery_name = ''; 

foreach ($suppliers as $supplier_name) 
{ 
$nursery_name .= ','.$supplier_name; 
} 

// output the column headings 
fputcsv($output, array($nursery_name)); 

foreach ($names as $pl_botanical_name) 
{ 
$nursery_plants .= $pl_botanical_name.","; 

$i=0; 
while($i < count($suppliers)) 
{ 
    if(array_key_exists($i,$price_by_id)) 
    {  
     if(in_array($pl_botanical_name,$botanical_name[$i])) 
     { 
      $q = array_search($pl_botanical_name,$botanical_name[$i]); 

      $ele = $price_by_id[$i][$q]; 
      $nursery_plants .= $ele .','; 

     } else { 
      $nursery_plants .= ','; 
     }    
    } else { 
     $nursery_plants .= ""; 
    } 
    $i++; 
} 
$nursery_plants .= "\n"; 
} 
fputcsv($output, array($nursery_plants)); 
+0

如果這些值包含',',它們必須用引號引起來,這樣它們不會被當作分隔符。 – Barmar

+0

沒有多數民衆贊成加入逗號(「,」)刪除它並不能解決我的問題 – user1531830

+0

如果$ nursery_plants字符串包含逗號,fputcsv函數將引號括起來的字符串。 – bitfiddler

回答

0

您不需要用逗號連接所有數據,這就是fputcsv()爲您所做的。你應該給它一個數組,其中每個元素是不同的列,並將它們用逗號分開。你應該寫:

// output the column headings 
array_unshift($suppliers, 'Name'); // First column is botanical name, before suppliers 
fputcsv($output, $suppliers); 

// output the data rows 
foreach ($names as $pl_botanical_name) 
    { 
    $nursery_plants = array($pl_botanical_name); 

    for ($i = 0; $i < count($suppliers); $i++) 
     { 
     if(array_key_exists($i,$price_by_id)) 
      {  
      if(in_array($pl_botanical_name,$botanical_name[$i])) 
       { 
       $q = array_search($pl_botanical_name,$botanical_name[$i]); 

       $ele = $price_by_id[$i][$q]; 
       $nursery_plants[] = $ele; 

       } else { 
       $nursery_plants[] = ''; 
      }    
      } else { 
      $nursery_plants[] = ""; 
     } 
     } 
    fputcsv($output, $nursery_plants); 
    } 
+0

你有錯誤我想:'fputcsv($ output,$ nursery_plants);'應該是最後一行**裏面的** foreach – LexLythius

+0

謝謝,修正。沒有注意到他連接了所有的行,而不僅僅是列。 – Barmar

+0

很棒,但名字仍然出現在第一列 – user1531830