Forum Moderators: coopster

Message Too Old, No Replies

Fetching Data from TEXT datatype field of MySql

         

compose

10:35 am on Nov 28, 2005 (gmt 0)

10+ Year Member



Hi,

I make a user registration form. I saved this data in table of mysql. Here is one field description in table this is of Text type. It stores details about company.

Now problem is that user enter data in description field with formatting means using newline charaters [line feed, tab]

but when i retrive data from table it shows Description field data in a single line with no formatting.

Please tell how to do this.

It is just like we are posting messages in forum. It saves with formatting like tabes, enters etc. and shows thread with same formatting.

Thanx,

tomda

11:01 am on Nov 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure if my solution is the best solution but you should functions. Again, this is how I am doing it. If anyone has a better solution or any comments regarding codes below, let us know.

The first one should be always use before inserting data in database. It replace <br> by a return+new_line. And it uses htmlentities to make it safe


function var_html_encode ($varia) {
$varia=rtrim($varia); $varia=ltrim($varia);
$varia=str_replace("<br>","\r\n",$varia);
$varia=htmlentities($varia,ENT_QUOTES,"utf-8");
return $varia; }

The second function below should be used when fetching data from database. As you can see, it uses html_entity_decode (inverse of htmlentities) and replace the return+new_line by a <br>.


function var_html_decode($varia) {
$varia=rtrim($varia);
$varia=ltrim($varia);
$varia=html_entity_decode($varia,ENT_QUOTES);
$varia=str_replace("\r\n","<br>",$varia);
return $varia;}

This function is slightly similar than the one above, the only difference is that it removes all HTML element, excepted <br>.


function var_html_tagdecode($varia) {
$varia=rtrim($varia);
$varia=ltrim($varia);
$varia=html_entity_decode($varia,ENT_QUOTES);
$varia=str_replace("\r\n","<br>",$varia);
$varia=strip_tags($varia, "<br>");
return $varia;}

Nutter

2:35 pm on Nov 28, 2005 (gmt 0)

10+ Year Member



nl2br [us3.php.net] will do what I think you're asking.

tomda

5:45 am on Nov 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Nutter, you are right! Almost forgot this one :)

compose

7:56 pm on Dec 9, 2005 (gmt 0)

10+ Year Member



Sorry, I was not able to access internet so not seen this reply,

Any ways thanx for ur replies. I applied ur solution and it is working fine. Thanx a lot.

Vineet