PHP输出到CSV问题

时间:2013-07-02 03:25:20

标签: php csv

我正在尝试将我的数据输出到我已成功完成的CSV文件中,但正在发生的是当您使用开放式办公室打开它时这些“出现在字段的开头和结尾处。 他们怎么能被删除?

我打开之前的数据没有“”。     

    
    "                         Alderwood Nursery      "
    "Delonix regia - 100ltr   0
    Delonix regia - 125ltr    3
    "
    
    
,Alderwood苗圃,All Green Nursery,Andreasens Green,Botanica Nurseries,Cabbage Tree Nursery,Carstens Gardens,E Plants Noosa,Exotic Nurseries and Landscaping,Greenstock Nurseries,Kenthurst Nursery,Logans Nursery,Mad About Plants,Meyer Nurseries,Murphy& Nowland Nursery,牛津公园苗圃,Pacific Trees Qld Pty Ltd,Pallara Trees,植物直接昆士兰

// 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));

1 个答案:

答案 0 :(得分: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);
  }