Another simple PHP problem has me stumped!

Associate
Joined
21 Sep 2007
Posts
453
Me again, this time with another problem I'm sure is super easy for you php eggheads to fix!

I have included the nl2br function on my add and edit pages, so the user doesnt have to use code to make new lines/breaks, its done for them.

Working nicely when i add a new article, yet when I goto edit an article, and update it, the code adds in the breaks again where the old ones are, doubling them up. If i edit it again, it would add the breaks a third time next to the original ones.

i.e.

Original article:

Code:
Hello

There

This would look like:

Code:
Hello<br />
<br />
There

if i edit it to read:

Code:
Hello

There

People!

it would end up:

Code:
Hello<br />
<br /><br /><br />
There<br />
<br />
People!

So firstly how can i stop it doing this? and Secondly, can I make it so when editing an article, the <br /> do not show up in the text box?

Thanks once again!
 
Why not use nl2br() only when you display the article, and not when you store it. i.e. use nl2br() when you're displaying the article as HTML on the public pages, otherwise leave it alone when editing in a <textarea>.
 
I understand what your saying, yet I have not much of a clue on how to implement it.

Below is the code I am using to output my articles:

PHP:
<?php

while($row = mysql_fetch_array($newsresult))
  {

echo "<tr><td class=\"newstitle\">";
echo $row['title'];
echo "</td><td class=\"newsdate\">";
echo $row['date'];
echo "</td></tr>";
echo "<tr><td colspan=\"2\" class=\"newscontent\">";
echo $row['content'];
echo "</td></tr>";

}

?>

also, if you need to know:

PHP:
$newsresult = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 0, 10")
or die(mysql_error());

Can you give me a pointer (i.e. a massive hint) where i would use nl2br?

Also, I still need the returns to be shown when editing an article in the admin area...
 
Back
Top Bottom