Page 1 of 1

Formatting the result of SQL query in HTML

Posted: Wed Jul 30, 2008 9:11 am
by lynchpin
Hi,

I have a page(page1) which dynamically displays the content of a mysql database column. The column takes input as text from a <textarea> field on another form on another page(page2).

The problem is when the result is diplayed in html on the first page(page2), it looses its initial formatting of pragraphs, spacing e.t.c. that was originally typed in the input page(page2). But I want the spacing and paragraghs to be displayed as well.

Any suggestions...

Thank you.

Re: Formatting the result of SQL query in HTML

Posted: Wed Jul 30, 2008 9:34 am
by jaoudestudios
Can you give us an example! I think I know what the problem could be.

Re: Formatting the result of SQL query in HTML

Posted: Wed Jul 30, 2008 10:12 am
by lynchpin
Thanks for the reply.

An example;

Supposing I have a page(index.php) that contains some information about my site. But i want the content(text) on the page to be loaded from a database column(db_pagecontent) of type text.

I also have another page (manager.php) where I have a <textarea> html field where I can view,edit or delete content and save it to (db_pagecontent).

When index.php load it queries my database and loads the text in db_pagecontent as html text.

sample code of index.php:
----------------------------------------------
<?php
include("connectx.php");
$query = "SELECT db_pagecontent FROM index";
$result= mysql_query($query,$cnx);
$rows = mysql_fetch_array($result);

?>
<html>
<body>
echo"$rows['db_pagecontent']";
</body>
</html>

---------------------------------------------------
Thank you.

Re: Formatting the result of SQL query in HTML

Posted: Wed Jul 30, 2008 10:32 am
by jaoudestudios
I was expecting to see what the error was, ie. text before and text after. So I can see the exact problem

I assume you are loosing the line breaks.

You will just need to do a replace all for all the '\r\n' to '<br>' (if xhtml standard) or '<br />' (if xhtml strick) before you display it to the screen and you will get the correct paragraph formating. :)

Re: Formatting the result of SQL query in HTML

Posted: Thu Jul 31, 2008 5:48 pm
by lynchpin
Thanks for that.

I wuld try it and get back to you.

NB This site has been having issues, sometimes I get a network error trying to load it, hence the late reply.

Re: Formatting the result of SQL query in HTML

Posted: Sun Aug 03, 2008 7:11 pm
by Bill H
Check the nl2br() function.