Jump to content


Photo

nl2br


  • Please log in to reply
10 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 April 2006 - 04:24 AM

Hi.

I retrieve news from my database.

and to print out, i use echo nl2br($row['news']).

But it doesnt work.

Then, i click "edit news" which takes me to a form with the news as the value in the text area.
Now i see the news with <br> where i had pressed enter when i submitted.
So all i have to do is click submit and itll update the database with the new news.

however, that doesnt sound right. I shouldnt have to submit the news, then edit it for the nl2br to work.

Does anyone know what's wrong?

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 01 April 2006 - 05:20 AM

1) What OS are you running (yes, it does matter because *NIX uses: \n, Windows uses \r\n, and Mac uses \r)
2) What code are you using to insert the information into the DB

#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 April 2006 - 06:21 AM

Hi and thanks!

It's a RedHat Linux server.

To insert, i have a form, and heres the php code for it:


<?php

if (isset($_POST['submit'])) {    
    $news = escape_data($_POST['news']);
    $title = escape_data($_POST['title']);
    
    $query = "INSERT INTO news (title, news, date_submitted, author) VALUES ('$title', '".wordwrap($news, 45, "\n", 1)."', NOW(), '". $_SESSION['user_name'] ."')";
    $result = mysql_query($query) or die (mysql_error());
    
    if ($result) {
        header ('Location: index.php');
    } else {
        echo 'Could not add news.';
    }
}

?>

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 01 April 2006 - 10:26 AM

Don't store formatted news in the DB. Store it from the textarea complete with "\n"s.

Format it when you output it.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 April 2006 - 10:07 PM

[!--quoteo(post=360583:date=Apr 1 2006, 05:26 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 1 2006, 05:26 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Don't store formatted news in the DB. Store it from the textarea complete with "\n"s.

Format it when you output it.
[/quote]


Hi Barand.

Are you talking about this: wordwrap($news, 45, "\n", 1) ?
Thats the only formatting i do when inserting news into the db (besides escaping quotes)


regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 01 April 2006 - 10:17 PM

If you are seing '<br>' in the text area when you pull it from the table then it sounds as though somewhere there is a nl2br($news) before it is saved to the table.

Also, re wordwrap - if you subsequently redesign the page and want to wrap to a different width then you're going to have some "unwrapping" to do first.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 April 2006 - 10:21 PM

Oh, Barand i think i didn't explain very well the problem, sorry.

here's exactly how it goes:

- I type the news in a text area and click submit and it puts it in the database

- I use am redirected to the index page, and notice that the news don't have any linebreaking at all. It's all crumbled up together.

- Then i click "edit", and i am taken to the edit form, but now i see <br> where i had pressed enter when i first entered the news. So since the <br>'s are there when i go edit it, i just click "submit updated news".


so it doenst show on the news page, but only when i edit.


Hope this helps better.



And i don't format it with nl2br before entering it in the db. here is the full code for "add news":
<?php
$title = 'Post news';
$header_title = 'Add news. . .';
include ('./includes/header.php');
if (!isset($_SESSION['user_name'])) {
    header ("Location: index.php");
}
?>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td>Title:</td>
        </tr>
        
        <tr>
            <td><input type="text" name="title"  /></td>
        </tr>
        
        <tr>
            <td></td>
        </tr>
        
        <tr>
            <td>News:</td>
        </tr>
        
        <tr>
            <td><textarea name="news" rows="7" cols="40"></textarea></td>
        </tr>
        
        <tr>
            <td><input type="submit" name="submit" value="post news" /></td>
        </tr>
    </table>
</form>


<?php

if (isset($_POST['submit'])) {    
    require_once ('./db_connect.php');
    $news = escape_data($_POST['news']);
    $title = escape_data($_POST['title']);
    
    $query = "INSERT INTO news (title, news, date_submitted, author) VALUES ('$title', '".wordwrap($news, 45, "\n", 1)."', NOW(), '". $_SESSION['user_name'] ."')";
    $result = mysql_query($query) or die (mysql_error());
    
    if ($result) {
        header ('Location: index.php');
    } else {
        echo 'Could not add news.';
    }
}

include ('./includes/footer.php');
?>

Escape data, goes as follows:
    function escape_data($data) {
        global $dbc;
        if (ini_get('magic_quotes_gpc')) {
            $data = stripslashes($data);
            } return mysql_real_escape_string(trim($data), $dbc);
        }

Ive also tried inserting it without wordwrap, to see if it was the problem, and it wasn't. same thing happens
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#8 graecyn

graecyn
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationWashington, DC

Posted 02 April 2006 - 04:37 AM

Nevermind!!! Covered already. :P
"I am always doing that which I cannot do, so that I may learn how to do it." -- Pablo Picasso

www.graecyn.com | www.beautifulgeek.com

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 02 April 2006 - 10:44 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]And i don't format it with nl2br before entering it in the db. here is the full code for "add news":[/quote]

In that case, are you using nl2br() when you put it into textarea for editing?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 05 April 2006 - 01:50 PM

[!--quoteo(post=360844:date=Apr 2 2006, 05:44 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 2 2006, 05:44 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
In that case, are you using nl2br() when you put it into textarea for editing?
[/quote]

Yes.

I suppose i should take that out huh?
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 05 April 2006 - 11:10 PM

You guessed right
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users