Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/72946-solved-displaying-text-from-mysql/
Share on other sites

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.