我想将表单数据存储到csv文件

时间:2015-09-04 09:26:58

标签: php csv export-to-csv

我正在尝试使用此创建文件我想在提交表单时将所有数据存储到.CVS文件中。我只获得前4个标签值,而我正在尝试使用添加更多并提供数据并在那时提交仅存储名称为school_1和university_1但未将数据存入.CVS文件的列名称。

save.php

<?php
$PAGENAME = basename(__FILE__);
If(isset($_POST['Submit'])) 
{
$today_date = date('m-d-Y', time());
include("processdata.php");
exportMysqlToCsv($today_date); 
header('Location:save.php'); 
exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="mdcstyle.css" />
<title>Submission form</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
var currentItem = 0;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><td>School:<input class="textfield"  name="school_'+currentItem+'" id =" school_'+currentItem+'" type="text"/></td><td>University:<input class="textfield"  name="university_'+currentItem+'" id ="university_'+currentItem+'" type="text"/></td></tr>';
$('#data').append(strToAdd);

});
});
</script> 
</head>
<body>
<div class="content">
<h2>header text</h2>
<p>Please complete this form for Udpates. Add a new line if you have more than one project.</p>
<form id="myform" name="form1" method="POST" action="<?php echo $PAGENAME; ?>">  
<table class="dd" width="100px" id="data">
<tr>
<td>Name:<input class="textfield" name="name" id="name" type="text" /></td>
<td>age:<input class="textfield"  name="age" id="age" type="text" /></td>
</tr>
<tr>
<td>School:<input class="textfield" name="school" id="school" type="text" /></td>
<td>University:<input class="textfield"  name="university" id="university" type="text" /></td>
</tr>
</table>
<input class="subbutton" type="submit" name="Submit" value="Submit Form"> 
</form>   
<button id="addnew" name="addnew" value="Add new item">Add new entry</button>
<input type="hidden" id="items" name="items" value="1" /> 
<br>
</div>
</body>
</html>
</html>

processdata.php

<?php
error_reporting(0);
function exportMysqlToCsv($today_date)
{
$filename = "csv_files/".$today_date.".csv";
if (!file_exists($filename))
{
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$columns = array('Name','Age','School','University');
$minus_counter = 0;
$minus_divider = 0;
if(isset($_POST['name']))
{
$minus_counter++;  
}
echo count($_POST)."</br>";
echo $minus_counter."</br>";
echo count($columns);
if((count($_POST)- (1 + $minus_counter) ) >= count($columns))
{
$more_fields =  ((count($_POST)- ($minus_counter) ) - count($columns))/2;
for($k=1;$k<=$more_fields;$k++)
{
$add=array_push($columns,'school_'.$k,'university_'.$k);
}
}
$schema_insert = '';
for ($i = 0; $i < count($columns); $i++)
{
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,$columns[$i]) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
//for($c=0;$c<(count($columns)- ((count($_POST)- (2 + $minus_counter)) - count($columns)));$c++)
for($c=0;$c<28;$c++)
{
$schema_insert = '';

if($columns[$c]=="Name")
{
$schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $_POST['name']) . $csv_enclosed;
}       
else if($columns[$c]=="Age")
{
$schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $_POST['age']) . $csv_enclosed;
}
else if($columns[$c]=="School")
{
$schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $_POST['school']) . $csv_enclosed;
}
else if($columns[$c]=="University")
{
$schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $_POST['university']) . $csv_enclosed;
}
$out .= $schema_insert;
$out .= $csv_separator;
} 
if(isset($_POST['name']) || isset($_POST['age']))
{
$plus_divider = 4;
}
else
{
$plus_divider = 6;
}
if((count($_POST)- ($minus_divider)) > count($columns))
{   
$more_fields =  (((count($_POST) + $plus_divider)- ($minus_divider) ) - count($columns))/2;
echo $more_fields;

for($k=1;$k<=$more_fields;$k++)
{
//echo $k;
$out .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $_POST['school_'.$k]) . $csv_enclosed;
$out .= $csv_separator;

$out .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $_POST['university_'.$k]) . $csv_enclosed;
$out .= $csv_separator;
}
}
if(isset($_POST['name']) || isset($_POST['age']))
{
$out .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, 'Yes') . $csv_enclosed;
$out .= $csv_separator; 
}
$out .= $csv_terminated;
$filename = date('m-d-Y', time()).".csv";
chmod($filename, 0777);
file_put_contents("csv_files/".$filename,$out);
$i++;
}
}
?>

0 个答案:

没有答案