Pains with Paragraphs

I have a form on my website meant to collect comments from a user. Once submitted the form data is saved to a csv file using this script
[php]<?php

$mm = $_POST['mm'];
$dd = $_POST['dd'];
$yy = $_POST['yy'];
$name = $_POST['name'];
$comment = $_POST['comment'];

$lines = file('datafeed/filetoread.csv');
$fp = fopen("datafeed/filetoread.csv", "w+");
$header = array_shift($lines);
$savestring = $header . $mm . "	" . $dd . "	" . $yy . "	" . $name . "	" . $comment . "\r\n";
fwrite($fp, stripslashes($savestring));

foreach($lines as $line) fwrite($fp, "$line");

fclose($fp);
echo "<h1>Your data has been saved</h1>";

?>
[/php]
I have another page on my site that reads the csv and displays the information using this script
[php]<?php

//create table with header and column names
print(’

’);
print(‘’);

//declare variable to count records
$rowcount = 0;

//get path to file assuming that file located in datafeed folder
$path_to_file=“datafeed/filetoread.csv”;

//open file for reading “r”
$handle = fopen($path_to_file, “r”);

//read file line by line. Enter tab between quotation marks
while (($record = fgetcsv($handle, 1000, " ")) !== FALSE) {

// read number of fields in one record
$numrecords = count($record);

//read field values in variable

$mm=$record[0];
$dd=$record[1];
$yy=$record[2];
$name=$record[3];
$comment=$record[4];

//skip the first record since it has field headers only
if($rowcount > 0)
{
//if record has link to item display it in the table row

		if($record[3] !="")
  		{
		print('<tr>
<td style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px;">'.$mm.'/'.$dd.'/'.$yy.'</td>
'); }
	}

$rowcount++;

}
fclose($handle);
print(’

Testimonials

'.$comment.'
'.$name.'
’);
?>[/php]
The problem I am having is that whenever a user submits a comment with more than one paragraph the second paragraph doesn’t appear when the csv is read by the second webpage. When I check the csv the 2nd paragraphs information is written on a new line. I need to figure out how to support paragraphs either in the writing of the information to the csv or in the reading of the file.

Any help is greatly appreciated

A messy fix would be to replace newlines in the code with another character or set of characters then replace it back when you read the data:

[php]// Writing
$comment = str_replace("\n", ‘–NL–’, $_POST[‘comment’]);[/php]

[php]// Reading
$comment = str_replace(’–NL–’, “\n”, $record[4]);[/php]

I dont understand

Maybe just get rid of new lines in the comment field, or replace it with html’s
before writing to csv file?
In the first piece of code, find this line:
[php]$header = array_shift($lines);[/php]
and after that line insert this code:
[php]$comment = str_replace("\n", “
”, $comment);
$comment = str_replace("\r", “”, $comment);[/php]

then leave the rest of your code as is.

AMAZING! It is finally fixed. Thank you so much for all the help. This was exactly the needed code to fix my problem!

Sponsor our Newsletter | Privacy Policy | Terms of Service