Page 1 of 1

Newline in a .txt file

Posted: Sun Dec 14, 2008 10:07 am
by oscardog
Well i've coded it all, and it all works perfectly(almost) they basically have a .txt file and they can add notes to that file. Im using a while loop to output the contents of the .txt file, but its all being classed as one line.

So when i put the note into the .txt file it looks like this:

Note1Note2Note3Note4

BUT when they put a new note in i want it to do...

Note1
Note2
Note3
Note4

I was wondering if you use "/n" but thats just a newline in the HTML code i thought so yeh any help? :)

Re: Newline in a .txt file

Posted: Sun Dec 14, 2008 10:16 am
by Eran
Actually <br /> is a line break in HTML. \n is a line-break in plain text, so you should put it where you want lines to break.

Re: Newline in a .txt file

Posted: Sun Dec 14, 2008 10:19 am
by oscardog
So my code should look like this:

(Add_note.php):

Code: Select all

$note = $_POST['note'];
$note = $note . "/n";
$handle = fopen($filename, 'a'); 
fwrite($handle, $note);
fclose($handle);
Then to display..:

Code: Select all

while(!feof($file))
  {
  echo "<tr>";
  echo "<td width=\"30\">$notecounter .</td>";
  echo "<td><div align=\"left\">" . fgets($file). "</div></td>";
  echo "</tr>";
  $notecounter++;
  }
fclose($file);
And then that will display one 'note' at a time?

EDIT: That didnt work, it just outputted it all as one line again...

Re: Newline in a .txt file

Posted: Sun Dec 14, 2008 10:39 am
by Eran
It's "\n" not "/n"

Re: Newline in a .txt file

Posted: Sun Dec 14, 2008 10:41 am
by oscardog
I should read more carefully, thanks it works now :)

Re: Newline in a .txt file

Posted: Sun Dec 14, 2008 11:11 am
by John Cartwright
There is always the cross-platform PHP_EOL constant.