繁体   English   中英

PHP输出到CSV问题

[英]PHP Output To CSV issue

我正在尝试将数据输出到我已经成功完成的CSV文件中,但是发生的情况是,当您在开放式办公室中打开这些文件时,这些“出现在字段的开头和结尾。如何将其删除?

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

"                         Alderwood Nursery      "
    "Delonix regia - 100ltr   0
    Delonix regia - 125ltr    3
    "
,艾德伍德苗圃,全绿苗圃,安德森森绿色,植物学苗圃,白菜苗圃,卡斯滕斯花园,E植物努萨,异国苗圃和环境美化,Greenstock苗圃,Kenthurst苗圃,Logans苗圃,疯狂的植物,Meyer苗圃,Murphy和Nowland苗圃,牛津公园苗圃,太平洋树木昆士兰私人有限公司,帕拉拉树木,植物直接昆士兰州

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

您不需要自己用逗号连接所有数据,这就是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);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM