Page 1 of 1

PHP data to .CSV

Posted: Thu Jun 11, 2009 4:51 am
by fopa
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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.

Re: PHP data to .CSV

Posted: Thu Jun 11, 2009 7:57 am
by sivaji
Use some string replacing commands to over come this problem ex... replace comma with underscore etc..... after that place that code in to csv...

Re: PHP data to .CSV

Posted: Thu Jun 11, 2009 9:37 am
by Reviresco
Perhaps this would do the trick?

Code: Select all

$string = str_replace(',', '&#44;', $string);

Re: PHP data to .CSV

Posted: Thu Jun 11, 2009 9:52 am
by fopa
Thanks for the reply's :)

Should i add that line in at line 17?

Itried the following, the suggested _ and the php does not process and display the thankyou page.

Code: Select all

$pfw_values = "$select,$Q1,$Q2,$Q3,$Q4,$Q5,$Q6,".str_replace ("\r\n","<BR>",$textarea )."_".str_replace ("\r\n"_"<BR>",$textarea2 )."\r\n";
Edit: Sorry pickle, and thanks for the pointer!

Re: PHP data to .CSV

Posted: Thu Jun 11, 2009 9:54 am
by pickle
The CSV format does allow for values to be delimited. Commonly that is with double quotes. Using addslashes() on the value first, would prevent the user entered values from escaping the quotes.

Re: PHP data to .CSV

Posted: Thu Jun 11, 2009 10:02 am
by fopa
Thanks I understand your reply in theory as such. So your suggesting something as follows?

Code: Select all

@$textarea = addslashes($_POST['"textarea"']);
@$textarea2 = addslashes($_POST['"textarea2"']);
I tried the above, but there was then no output to the .csv for those two fields. Sorry im new to PHP.

Re: PHP data to .CSV

Posted: Thu Jun 11, 2009 10:13 am
by pickle
fopa wrote:

Code: Select all

@$textarea = addslashes($_POST['"textarea"']);
@$textarea2 = addslashes($_POST['"textarea2"']);
Close:

Code: Select all

$textarea = '"'.addslashes($_POST['textarea']).'"';
$textarea2 = '"'.addslashes($_POST['textarea2']).'"';
You'll need to delimit the other fields in double quotes as well - it's an all or nothing thing. Also, take off the @ everywhere. That stops errors from being displayed - which isn't very helpful while you're still building a script.

Re: PHP data to .CSV

Posted: Thu Jun 11, 2009 10:26 am
by fopa
Thankyoui, that has worked perfectly, i have removed the @ signs also.

Thanks again your help really is apprecaited.