PHP输出到CSV问题(PHP Output To CSV issue)

编程入门 行业动态 更新时间:2024-10-17 07:28:20
PHP输出到CSV问题(PHP Output To CSV issue)

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

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

" Alderwood Nursery " "Delonix regia - 100ltr 0 Delonix regia - 125ltr 3 " ,Alderwood苗圃,所有绿色苗圃,Andreasens Green,Botanica苗圃,白菜树苗圃,Carstens花园,E植物Noosa,异国托儿所和园林绿化,Greenstock苗圃,Kenthurst苗圃,Logans苗圃,Mad About Plants,Meyer苗圃,Murphy&Nowland苗圃,牛津公园苗圃,太平洋树木Qld Pty有限公司,Pallara树,植物直接昆士兰

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

I am trying to output my data to a CSV file which i have done successfully, but what is happening is the when you open it with open office these " appear at the beginning and end of the fields. How can they be removed?

my data before opened has no " ".

" Alderwood Nursery " "Delonix regia - 100ltr 0 Delonix regia - 125ltr 3 " ,Alderwood Nursery,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,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));

最满意答案

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

You don't need to connect all the data with commas yourself, that's what fputcsv() does for you. You're supposed to give it an array where each element is a different column, and it will separate them with commas. You should write:

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

更多推荐

本文发布于:2023-08-06 16:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CSV   PHP   issue   Output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!