Irksome Posted February 11, 2009 Share Posted February 11, 2009 OK it's been a while since I've done any PHP so I'm trying to create a quick news script that contains user, title and text. Now the text field needs to be compatible with paragraphs, but it currently isn't. Everything comes out on one line. I know it's nl2br I need, but I've read through a few tutorials and code snippets but I just can't figure out how to apply it to my script. My code for my addnews.php page is here: <?php session_start(); include("config.php"); if($submit) {//begin of if($submit). $title = mysql_real_escape_string($_POST['title']); $user = mysql_real_escape_string($_POST['user']); $text2 = mysql_real_escape_string($_POST['text2']); $user = Trim(stripslashes($_POST['user'])); $text2 = Trim(stripslashes($_POST['text2'])); // nl2br $Body = ""; $Body .= "Comments: "; $Body .= $text2; $Body .= "\n"; //run query $result = mysql_query("INSERT INTO scnews (title, dtime, user, text2) VALUES ('$title',NOW(),'$user','$text2')",$connect); //success message. echo "<b>News entry added successfully!<br>"; echo "<meta http-equiv=Refresh content=0;url=index.php>"; }//end of if($submit). else {//begin of else ?> <br> <h3>Add news article</h3> <form method="post" action="<?php echo $PHP_SELF ?>"> Title: <input name="title" size="40" maxlength="255"> <br> Logged on user: <b><?php echo $_SESSION['username']; ?></b> <input type="hidden" name="user" size="40" maxlength="255" value="<?php echo $_SESSION['username']; ?>"/> <br> Text:<br> <textarea name="text2" id="text2" class="ed"></textarea> <br> <input type="submit" name="submit" value="Add News Article"> </form> <? }//end of else ?> If anyone could sort of point me in the right direction here it would be greatly appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/144808-solved-nl2br/ Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 You only want to use nl2br when displaying content on a page as actual content. To store it, you want it in it's raw form, meaning leaving the \n's there. This goes true for putting it into a textarea for editing, you want the \n's there. Quote Link to comment https://forums.phpfreaks.com/topic/144808-solved-nl2br/#findComment-759870 Share on other sites More sharing options...
Irksome Posted February 11, 2009 Author Share Posted February 11, 2009 Oh I see, so would I apply the same as in the above bit of code to the page that displays the news? The code for that page is here: <html> <body> test page<br> <br> <?php session_start(); include 'config.php'; $result = mysql_query("SELECT * FROM scnews ORDER BY newsid DESC",$connect); //lets make a loop and get all news from the database while($myrow = mysql_fetch_assoc($result)) {//begin of loop //now print the results: echo '<b><font face=tahoma size=3 color="#5f88c0">'; echo $myrow['title']; echo "</b></span></font><font face=tahoma size=2><br><br>"; echo $myrow['text2']; echo "<br><hr width=220 align=left>Posted By: <b>"; echo $myrow['user']; echo "</b> On <i>"; echo $myrow['dtime']; echo "</i><br>"; }//end of loop ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/144808-solved-nl2br/#findComment-759878 Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 echo nl2br($myrow['text2']); That should be the only line that needs it done, as I assume title does not allow for linebreaks. Quote Link to comment https://forums.phpfreaks.com/topic/144808-solved-nl2br/#findComment-759883 Share on other sites More sharing options...
Irksome Posted February 11, 2009 Author Share Posted February 11, 2009 Ah that's worked, didn't think it would be so simple as that. Thanks a lot premiso. Quote Link to comment https://forums.phpfreaks.com/topic/144808-solved-nl2br/#findComment-759885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.