0

我正在尝试将我的数据输出到我已经成功完成的 CSV 文件,但是当你用 open office 打开它时,这些“出现在字段的开头和结尾。如何删除它们?

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

    
    《桤木苗圃》
    “帝王蝶 - 100ltr 0
    Delonix regia - 125ltr 3
    "
    
    
,Alderwood 苗圃,All Green 苗圃,Andreasens Green,Botanica 苗圃,卷心菜苗圃,Carstens 花园,E 植物 Noosa,异国苗圃和景观美化,Greenstock 苗圃,Kenthurst 苗圃,Logans 苗圃,疯狂的植物,Meyer 苗圃,Murphy & Nowland 苗圃,Oxford Park Nursery,Pacific Trees Qld Pty Ltd,Pallara Trees,Plants Direct Queensland

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

1 回答 1

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);
  }
于 2013-07-02T03:38:13.067 回答