我尝试将表单中的数据插入到csv文件中,插入工作正常,但我对新行有问题,我做了不同的搜索,并且有不同的解决方案,但对我没有任何作用
这是代码
$file = fopen('contacts.csv', 'a+');
// open the file and check for a character
if( fread($file, 1) != "" ){
// file has data, write new line
fwrite($file, "\n");
}else{
// file doesnt have data, no new line needed
}
$csv=array();
$csv[] = $_POST['txt_number'];
$csv[] = $_POST['txt_stage'];
$csv[] = $_POST['txt_category'];
$csv[] = $_POST['txt_description'];
$csv[] = $_POST['txt_division'];
$csv[] = $_POST['txt_notapprove'];
$csv[] = $_POST['txt_approve'];
$csv[] = $_POST['txt_notexist'];
$csv[] = $_POST['txt_priority'];
$csv[] = $_POST['txt_frequency'];
$csv[] = $_POST['startdate'];
$csv[] = $_POST['enddate'];
$csv[] = $_POST['duration'];
$csv[] = $_POST['txt_asd'];
$csv[] = $_POST['txt_add'];
$csv[] = $_POST['txt_aduration'];
$csv[] = $_POST['txt_transferredto'];
$csv[] = $_POST['txt_wo'];
$csv[] = $_POST['txt_percentage'];
$csv[] = $_POST['txt_note'];
// put data stored in $csv into file
fputcsv($file,$csv);
fclose($file);
我想要的文件输出就像这个例子
,,,test,test,,,,,,
换新日期新行
,,,test2,test2,,,,,
我使用了不同的东西"\n","\r","\n\r"
但这个解决方案不起作用
一旦它给了我一个没有cama ","
的新行的结果,那就是我不想要的
比如何解决这个问题到我想要的外观csv文件中的新行?
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Description</label>
<div class="col-sm-3">
<input id="textinput" name="txt_description" type="text" placeholder="Description" class="form-control input-md">
</div>
<label class="col-sm-3 control-label" for="textinput">Division</label>
<div class="col-sm-3">
<input id="textinput" name="txt_division" type="text" placeholder="Division" class="form-control input-md" >
</div></div>
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Stage</label>
<div class="col-sm-3">
<input id="textinput" name="txt_stage" type="text" placeholder="Stage" class="form-control input-md" >
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textinput">Category</label>
<div class="col-sm-3">
<input id="textinput" name="txt_category" type="text" placeholder="Category" class="form-control input-md" >
</div>
<label class="col-md-3 control-label" for="radios">Priority</label>
<div class="col-sm-3">
<select id="selectbasic" name="txt_priority" class="form-control">
<option value=""></option>
<option value="Low Priority">Low Priority</option>
<option value="Routine">Routine</option>
<option value="Urgent">Urgent</option>
<option value="Important">Important</option>
</select>
</div>
</div>
<label class="col-sm-3 control-label" for="textinput">Frequency</label>
<div class="col-sm-3">
<select id="selectbasic" name="txt_frequency" class="form-control">
<option value=""></option>
<option value="Once">Once</option>
<option value="Daily">Daily</option>
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
<option value="Semiannual">Semiannual</option>
</select>
</div>
<label class="col-sm-3 control-label" for="textinput">Transferred To</label>
<div class="col-sm-3">
<?php
$q=mysqli_query($conn,"SELECT concat(db_fname,' ' , db_lname) as fname from tbl_user where db_level!='4'
UNION
SELECT db_CompanyName as scn from tbl_supplier
UNION
SELECT db_CompanyName as sucn from tbl_subcontractor
")or die(mysqli_error($conn));
echo'<select name="txt_transferredto" class="form-control inpu-md" >';
echo"<option value=''></option>";
while($row=mysqli_fetch_array($q)){
$fname=$row['fname'];
$companyname=$row['scn'];
$subcompanyname=$row['sucn'];
if($fname!=""){
echo"<option value='$fname'>";echo $fname;echo"</option>";}
else if($subcompanyname!=""){
echo"<option value='$subcompanyname'>";echo $subcompanyname;echo"</option>";}
else if($companyname!=""){
echo"<option value='$companyname'>";echo $companyname;echo"</option>";}
}
echo'</select>';
?>
</div>
<label class="col-sm-3 control-label" for="textinput">WO</label>
<div class="col-sm-3">
<input id="textinput" name="txt_wo" type="text" placeholder="WO" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="checkbox">Not Approve</label>
<div class="col-sm-3">
<input type = 'Radio' Name ='exist' value= 'Not Approve' >
</div>
<label class="col-sm-4 control-label" for="checkbox">Approve</label>
<div class="col-sm-3">
<input type = 'Radio' Name ='exist' value= 'Approve'>
</div>
<label class="col-sm-4 control-label" for="checkbox">Not Exist</label>
<div class="col-sm-3">
<input type = 'Radio' Name ='exist' value= 'Not Exist' >
</div>
<label class="col-sm-3 control-label" for="textinput">Percentage</label>
<div class="col-sm-3">
<input id="textinput" name="txt_percentage" type="text" placeholder="Percentage" class="form-control input-md">
</div>
</div>
<!--Planing Date-->
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Planing Start Date</label>
<div class="col-sm-3">
<input id="basic_example_1" name="startdate" type="text" placeholder="planing Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onChange="onDateChange()" onchange="cal()">
</div>
<label class="col-sm-3 control-label" for="textinput">Planing DUE Date</label>
<div class="col-sm-3">
<input id="basic_example_2" name="enddate" type="text" placeholder="planing Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onchange="cal()" >
</div>
<label class="col-sm-3 control-label" for="textinput" >Duration</label>
<div class="col-sm-3">
<input id="duration" name="duration" type="text" placeholder="Duration" class="form-control input-md" onChange="onDateChange()" >
</div>
</div>
<!--Actual Date -->
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Actual Start Date</label>
<div class="col-sm-3">
<input id="basic_example_3" name="txt_asd" type="text" placeholder="Actual Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onChange="onDateChange()" onchange="call()" >
</div>
<label class="col-sm-3 control-label" for="textinput">Actual DUE Date</label>
<div class="col-sm-3">
<input id="basic_example_4" name="txt_add" type="text" placeholder="Actual Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onchange="call()" >
</div>
<label class="col-sm-3 control-label" for="textinput" >Duration</label>
<div class="col-sm-3">
<input id="aduration" name="txt_aduration" type="text" placeholder="Duration" class="form-control input-md" onChange="onDateChange()" >
</div>
</div>
<!--progress Bar-->
<!-- Textarea -->
<div class="form-group">
<label class="col-md-3 control-label" for="textarea">Notes</label>
<div class="col-md-9">
<textarea class="form-control" id="textarea" name="txt_note" ></textarea>
</div>
</div>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-3 control-label" for="button1id"></label>
<div class="col-md-8">
<input type="submit" name="submit" value="Save" class="btn btn-success">
</div>
答案 0 :(得分:0)
将CSV格式化为多维数组。第一级索引表示行索引,第二级索引表示列索引。
$csv = [
[
'test',
'test',
'test',
],
[
'test2',
'test2',
'test2',
]
];
要写入CSV使用循环并逐行写入:
$file = fopen('contacts.csv', 'a+');
foreach ($csv as $row) {
fputcsv($file, $row);
}
fclose($file);
比你输出
test,test,test
test2,test2,test2
答案 1 :(得分:0)
除非你需要csv中不同顺序的字段而不是表单中显示的字段,否则我在这里忽略了这一点,以下工作 - 每次提交表单时,它都会用$ _POST数据写一个新行
$filename = __DIR__ . '/contacts.csv';
$file = fopen( $filename, 'a+' );
fputcsv( $file, $_POST );
fclose( $file );
要测试每个条目是否在新行上,请在上面的代码之后尝试添加:
$lines=file( $filename );
foreach( $lines as $line )echo $line.'<br />';
这表示之后每行都使用br
标记进行渲染,因此您可以假设正确识别行结尾,因此您应该能够使用它来输入数据库。
另一种方法,可以在记事本中正确显示的新行上生成每个$ _POST。
$filename=__DIR__.'/contacts.csv';
file_put_contents( $filename, '"' . implode( '","', $_POST ). '"' . PHP_EOL, FILE_APPEND | FILE_TEXT );
答案 2 :(得分:0)
解决方案是这个代码工作没有使用记事本来查看结果如果你想要你也可以使用PHP_EOL
它可以在记事本和其他编辑器上工作,感谢所有的回复,或者你可以使用PHP_EOL或\ n