Jump to content


Photo

Formatted output from SQL


  • Please log in to reply
2 replies to this topic

#1 DIMDIGITAL

DIMDIGITAL
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 April 2006 - 10:46 PM

I'm making this site and I'm having a problem with output. The users can type in a text that is saved to the sql database and the text is later outputted using the php echo function. My main problem is when out putting the text everything runs togethere in the sense that there are no new paragraphs.

For example, this current sentence that you are reading has started a new paragraph with a space between this one and the previous one above. In my case the two paragraphs would be clumped togethere with no space. I think I need to some how save sometype of formatting information in the string.

I was testing it and I do know that if I'm typing in the input box and I put html tags like <p><p/>, <span>, etc, when the table(sql) contents are printed out using <?php echo $var_name ?> the html tags are respected and not shown as text. Like if I put hello <p></p> hello. The second hello will start a new paragraph, but I dont think I want users putting html code into the input box so the formatting works.

How can I solve this issue?

Thanks

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 April 2006 - 10:52 PM

Basically, put what users enter into your database 'as is' and then when you display it use the [a href=\"http://ca.php.net/manual/en/function.nl2br.php\" target=\"_blank\"]nl2br() function[/a] to convert 'new lines' into html <br> tags:
$nice_text = nl2br($text);
echo $nice_text;

Legend has it that reading the manual never killed anyone.
My site

#3 DIMDIGITAL

DIMDIGITAL
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 April 2006 - 11:03 PM



Cool thanks it works. Thanks alot




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users