Jump to content

nl2br


play_

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/6317-nl2br/
Share on other sites

Hi and thanks!

It's a RedHat Linux server.

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

[code]

<?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.';
    }
}

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/6317-nl2br/#findComment-22825
Share on other sites

[!--quoteo(post=360583:date=Apr 1 2006, 05:26 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 1 2006, 05:26 AM) [snapback]360583[/snapback][/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)

Link to comment
https://forums.phpfreaks.com/topic/6317-nl2br/#findComment-22978
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/6317-nl2br/#findComment-22986
Share on other sites

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":
[code]
<?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');
?>
[/code]

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

Ive also tried inserting it without wordwrap, to see if it was the problem, and it wasn't. same thing happens
Link to comment
https://forums.phpfreaks.com/topic/6317-nl2br/#findComment-22989
Share on other sites

[!--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?
Link to comment
https://forums.phpfreaks.com/topic/6317-nl2br/#findComment-23111
Share on other sites

[!--quoteo(post=360844:date=Apr 2 2006, 05:44 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 2 2006, 05:44 AM) [snapback]360844[/snapback][/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?
Link to comment
https://forums.phpfreaks.com/topic/6317-nl2br/#findComment-24180
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.