doutang2017
2018-04-14 11:16
浏览 66
已采纳

通过PHP将标头添加到CSV文件

I have the following PHP script for creating CSV and collecting data from HTML form to CSV file, but I can't add headers to the first row of CSV file. I'm a newbie in PHP world so I'll appreciate if anyone can use this code to help me with that:

<?php

$fieldA = $_POST["prezime"];
$fieldB = $_POST["ime"];
$fieldC = $_POST["datumrodjenja"];
$fieldD = $_POST["mestorodjenja"];
$fieldE = $_POST["rod"];
$fieldF = $_POST["prebivaliste"];
$fieldG = $_POST["brojpasosa"];
$fieldH = $_POST["izdatod"];
$fieldI = $_POST["vazido"];
$fieldJ = $_POST["profesija"];
$fieldK = $_POST["zanimanje"];
$fieldL = $_POST["fiksni"];
$fieldM = $_POST["mobilni"];
$fieldN = $_POST["email"];
$fieldO = $_POST["napomena"];

$keys = array($fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM,$fieldN,$fieldO); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)

$fname = 'prijave.csv';
$fp = fopen($fname,'a');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

fputcsv($fp, $keys);
fclose($fp);

?>

图片转代码服务由CSDN问答提供 功能建议

我有以下PHP脚本用于创建CSV并将数据从HTML表格收集到CSV文件,但我不能 将标头添加到CSV文件的第一行。 我是PHP世界的新手,所以如果有人可以使用这段代码来帮助我,我将不胜感激:

 &lt;?php 
 
 $ fieldA =  $ _POST [“prezime”]; 
 $ fieldB = $ _POST [“ime”]; 
 $ fieldC = $ _POST [“datumrodjenja”]; 
 $ fieldD = $ _POST [“mestorodjenja”]; 
 $  fieldE = $ _POST [“rod”]; 
 $ fieldF = $ _POST [“prebivaliste”]; 
 $ fieldG = $ _POST [“brojpasosa”]; 
 $ fieldH = $ _POST [“izdatod”]; \  n $ fieldI = $ _POST [“vazido”]; 
 $ fieldJ = $ _POST [“profesija”]; 
 $ fieldK = $ _POST [“zanimanje”]; 
 $ fieldL = $ _POST [“fiksni”]  ; 
 $ fieldM = $ _POST [“mobilni”]; 
 $ fieldN = $ _POST [“email”]; 
 $ fieldO = $ _POST [“napomena”]; 
 
 $ keys = array($  FIELDA,$ fieldB,$ fieldC,$ fieldD,$ fieldE,$ fieldF,$ fieldG,$ fieldH,$ fieldI,$ fieldJ,$ fieldK,$字段1,$ fieldM,$字段N,$ FIELD0中);  //这就是你把表格元素放在ex:array('$ fieldA','$ fieldB'等)
 
 $ fname ='prijave.csv'; 
 $ fp = fopen($ fname,'  a'); 
 //添加BOM以修复Excel 
fputs中的UTF-8($ fp,$ bom =(chr(0xEF)。chr(0xBB)。chr(0xBF))); 
 
fputcsv($  fp,$ keys); 
 nclclose($ fp); 
 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题