我有一个HTML表单,比如调查,并且有一个包含此代码的PHP文件
<?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)
$csv_line = $keys;
foreach( $keys as $key ){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'prijave.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = $csv_line."\r\n" ;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$csv_line);
fclose($fcon);
?>
此代码的问题在于它创建.csv文件,但不超过2行。当从表单填充2行信息时,不再收集。
第二个问题,如何通过此脚本从表单中收集šćđč字母?谢谢!
答案 0 :(得分:0)
我对我正在进行的项目有类似的要求。我想出了一个小函数来将数组写入CSV文件(逗号分隔)。
我使用了fputcsv()功能非常方便。
该函数接受2个参数,数组名称以及要调用CSV文件的名称。使用fopen,我打开要保存CSV文件的目录中的空文件。
如果需要,我设置标题并将其写入文件。
然后我循环遍历数组并输出每个元素并用“,”分隔它。
完成后,我关闭文件。
我知道这可能无法解答您的完整问题,但我相信这会对您有所帮助。
如果您的数组看起来与下面的格式相同,那么我提供的功能将起作用。
<强>阵列强>
Array
(
[0] => John
[1] => Doe
[2] => England
[3] => Somewhere
[4] => Something
[5] => UK
[6] => Foo
[7] => Bar
[8] => Yes
[9] => Developer
[10] => Stack Overflow
[11] => No
[12] => 0123456778
[13] => johndoe@gmail.com
[14] => Smith
)
<强>功能强>
<?php
//Change these strings back to what you need
$fieldA = "John";
$fieldB = "Doe";
$fieldC = "England";
$fieldD = "Somewhere";
$fieldE = "Something";
$fieldF = "UK";
$fieldG = "Foo";
$fieldH = "Bar";
$fieldI = "Yes";
$fieldJ = "Developer";
$fieldK = "Stack Overflow";
$fieldL = "No";
$fieldM = "0123456778";
$fieldN = "johndoe@gmail.com";
$fieldO = "Smith";
$array = array( $fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM,$fieldN,$fieldO );
//set the filename
$filename = "prijave";
//call the function and pass the above array and filename as arguments
array_to_csv( $array, $filename );
//function to write array to csv file using fputcsv()
function array_to_csv( $array, $filename ) {
//open the csv file
//set the path for the file - use a+ to set file pointer to end of file
$out = fopen( "/path/to/output/directory/".$filename.".csv" , 'a+' );
//header - optional, remove if not needed
$header = array( "Column 1 Name","Column 2 Name","Column 3 Name","Column 4 Name","Column 5 Name","Column 6 Name",
"Column 7 Name","Column 8 Name","Column 9 Name","Column 10 Name","Column 11 Name","Column 12 Name",
"Column 13 Name","Column 14 Name","Column 15 Name"
);
//write the header - optional, remove if not needed
fputcsv ( $out, $header, "," );
//foreach loop is if you have a multidimensional array- uncomment if it is
//foreach( $array as $line ) {
//write each line of the array to csv file
fputcsv( $out, $array, "," );
//}
//close the file
fclose( $out );
}
?>
我已经测试了上面的代码,它可以正常工作。我有一个写入文件的标题,以及每列下面数组的值。