PHP data to .CSV
Posted: Thu Jun 11, 2009 4:51 am
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hi All,
I have a HTML form, which is processed via php to store the results in a .csv file. On the form itself i have to two text fields for comments. However when the user uses a comma in the text field it writes the data to the next cell within the CSV. Ideally i need all the comments to be displayed within the single cell.
Somebody mentioned that there is a work around using backslashes, and i will continue to research that, however could anybody provide a suggestion as to how i can achieve my single cell storing of comments when commas are inputted by the user.
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hi All,
I have a HTML form, which is processed via php to store the results in a .csv file. On the form itself i have to two text fields for comments. However when the user uses a comma in the text field it writes the data to the next cell within the CSV. Ideally i need all the comments to be displayed within the single cell.
Somebody mentioned that there is a work around using backslashes, and i will continue to research that, however could anybody provide a suggestion as to how i can achieve my single cell storing of comments when commas are inputted by the user.
Code: Select all
<?php
// Receiving variables
@$select = addslashes($_POST['select']);
@$Q1 = addslashes($_POST['Q1']);
@$Q2 = addslashes($_POST['Q2']);
@$Q3 = addslashes($_POST['Q3']);
@$Q4 = addslashes($_POST['Q4']);
@$Q5 = addslashes($_POST['Q5']);
@$Q6 = addslashes($_POST['Q6']);
@$textarea = addslashes($_POST['textarea']);
@$textarea2 = addslashes($_POST['textarea2']);
// Validation
//saving record in a text file
$pfw_file_name = "data.csv";
$pfw_first_raw = "select,Q1,Q2,Q3,Q4,Q5,Q6,textarea,Language,textarea2\r\n";
$pfw_values = "$select,$Q1,$Q2,$Q3,$Q4,$Q5,$Q6,".str_replace ("\r\n","<BR>",$textarea ).str_replace ("\r\n","<BR>",$textarea2 )."\r\n";
$pfw_is_first_row = false;
if(!file_exists($pfw_file_name))
{
$pfw_is_first_row = true ;
}
if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {
die("Cannot open file ($pfw_file_name)");
exit;
}
if ($pfw_is_first_row)
{
if (fwrite($pfw_handle, $pfw_first_raw ) === FALSE) {
die("Cannot write to file ($pfw_filename)");
exit;
}
}
if (fwrite($pfw_handle, $pfw_values) === FALSE) {
die("Cannot write to file ($pfw_filename)");
exit;
}
fclose($pfw_handle);
header("Location: thankyou.html");
?>pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: