PHP data to .CSV

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fopa
Forum Newbie
Posts: 7
Joined: Thu Jun 04, 2009 3:37 am

PHP data to .CSV

Post 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.
sivaji
Forum Newbie
Posts: 4
Joined: Thu Jun 11, 2009 12:41 am

Re: PHP data to .CSV

Post 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...
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: PHP data to .CSV

Post by Reviresco »

Perhaps this would do the trick?

Code: Select all

$string = str_replace(',', '&#44;', $string);
fopa
Forum Newbie
Posts: 7
Joined: Thu Jun 04, 2009 3:37 am

Re: PHP data to .CSV

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP data to .CSV

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
fopa
Forum Newbie
Posts: 7
Joined: Thu Jun 04, 2009 3:37 am

Re: PHP data to .CSV

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP data to .CSV

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
fopa
Forum Newbie
Posts: 7
Joined: Thu Jun 04, 2009 3:37 am

Re: PHP data to .CSV

Post by fopa »

Thankyoui, that has worked perfectly, i have removed the @ signs also.

Thanks again your help really is apprecaited.
Post Reply