Formatting the result of SQL query in HTML

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
lynchpin
Forum Commoner
Posts: 60
Joined: Mon Jul 21, 2008 1:31 pm

Formatting the result of SQL query in HTML

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Formatting the result of SQL query in HTML

Post by jaoudestudios »

Can you give us an example! I think I know what the problem could be.
lynchpin
Forum Commoner
Posts: 60
Joined: Mon Jul 21, 2008 1:31 pm

Re: Formatting the result of SQL query in HTML

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Formatting the result of SQL query in HTML

Post 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. :)
lynchpin
Forum Commoner
Posts: 60
Joined: Mon Jul 21, 2008 1:31 pm

Re: Formatting the result of SQL query in HTML

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Formatting the result of SQL query in HTML

Post by Bill H »

Check the nl2br() function.
Post Reply