Helminthophobe Posted October 12, 2007 Share Posted October 12, 2007 I apologize if this question has been asked before. I have read through some of the tutorials and tried to do some google searching but my knowledge of the terminology is a lacking and I'm usually lost in the results I find. I am working on a very simple news page for a website. I've setup a simple form that writes news to my database and the data is then displayed on my news page. I have gotten everything to work successfully but the content in the article loses all its paragraph formatting when I try to display it. I can use break tags to make the formatting show up but there has to be an easier way. This is the code I am using now, which I found on a tutorial somewhere and then tried to modify to work for my site: <?php mysql_connect("mysql", "username", "password") or die ("Error..."); mysql_select_db("database"); $query = mysql_query("SELECT * FROM news"); while ($row = mysql_fetch_array($query)) { echo $row['author']; echo "<br>"; echo $row['date']; echo "<br>"; echo $row['content']; } ?> Any help would be greatly appreciated. Thank you for your time, Helminthophobe Quote Link to comment https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/ Share on other sites More sharing options...
pocobueno1388 Posted October 12, 2007 Share Posted October 12, 2007 Before you insert the news content into the database, use nl2br() on it. www.php.net/nl2br Quote Link to comment https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/#findComment-367894 Share on other sites More sharing options...
GingerRobot Posted October 12, 2007 Share Posted October 12, 2007 Before you insert the news content into the database, use nl2br() on it. www.php.net/nl2br Most people would usually use the function prior to the display of the text, rather than before you put it in the database. Afterall, the reason you need to use the function is to format the display in HTML. The original text didn't contain break line tags, so you shouldn't store those in the database. Quote Link to comment https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/#findComment-367898 Share on other sites More sharing options...
harristweed Posted October 12, 2007 Share Posted October 12, 2007 I'd do this: while ($row = mysql_fetch_array($query)) { echo "<p>".$row['author']."</p>\n"; echo "<p>".$row['date']."</p>\n"; echo "<p>".$row['content']."</p>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/#findComment-367900 Share on other sites More sharing options...
Helminthophobe Posted October 12, 2007 Author Share Posted October 12, 2007 So once I have the information from the form and I'm writing to the database I would use nl2br($n_news) instead of just plain $n_news. I looked at the site you linked and it shows /n being turning into break tags. Does this require the input in the form to have /n or does it recognize the hard returns as /n automatically? Hopefully that made sense... Does the input from the form need to look like this... This is paragraph one./n /n This is paragraph two. or can it look like this? This is paragraph one. This is paragraph two. I would post the code to my script that writes to the database but I'm blocked from getting to my host provider's site at work... Thank you so much for the quick replies and help so far. Quote Link to comment https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/#findComment-367922 Share on other sites More sharing options...
pocobueno1388 Posted October 12, 2007 Share Posted October 12, 2007 If you use nl2br() before adding the content to the database, the form can look like your second way. This is paragraph one. This is paragraph two. I'm not sure if there is a better method, but this is how I always do it and it works fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/#findComment-367929 Share on other sites More sharing options...
Helminthophobe Posted October 12, 2007 Author Share Posted October 12, 2007 Awesome! Thanks for the help. When I get home this afternoon I will try this out. If it works, I will set the topic to solved. Quote Link to comment https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/#findComment-367934 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.