PHP. How to display a text from database with proper format?

when you display your data remember you're displaying it through a browser which in turns understands html. So you'll have to format it manually

echo " <p>".$mysalutaion."</p>";
echo "<p>".$mymessagebody."</p>";
echo "<p>".$myending."</p>";

Myself - I would created a php function to do a specific format if more than one entry will be formatted properly so I won't have to type it every time. You can also add styling with css, so be creative.

function formatmessage ( $hdr, $msg, $closing )
{
$myform = "<p>".$hdr."</p>;
$myform .= "<p>".$msg."</p>";
$myform .= "<p>".$closing."</p>";
return $myform;
}
 
I am trying to make a simple blog. I wrote something like:
-----------------
Hi,

I am happy to make my first blog......

Cheers,

Pisey
--------------

Then when I read the data from the database and displayed, it displayed in only one line without new line:

Hi, I am happy to make my first blog...... Cheers, Pisey.


So how can I solve this problem?

Looking forward.

Thanks in advance.
 
there is a function called nl2br in php which can be used to replace newline characters with a </br> tag, tabs and whitespaces will be preserved.

eg:

$string = "whatever\nwhatever\nwhatever";
echo nl2br($string);

will give:
whatever<br/>whatever<br/>whatever

which will ultimately display as:
whatever
whatever
whatever
 
Back
Top