Page 1 of 1

Insert and retrieved html and css formated text in Database

Posted: Thu Dec 08, 2011 1:13 pm
by MikeSpider
Hi guys,
How to keep styled and formated text in db and retrieve it?
My CMS has an "insert post" option. Where a title, a text and a picture can be inserted.
All this is inserted in databaSe, AND is retrieved in the home page.

The text just appears as simple text, no formating. all spaces and line breaks are ignored.
I tried installing a Rich text editor and name all the textareas after it but it was a failed attempt.
so I want to be able to insert html text.
for example.

<h2>Winter Party</h2>
<br>
<br>
<p style="color:red">Don' miss it!!!</p>
<p>some text some text some text, some text <u>some</u> text
<br>
some text.......
</p>


I heard that it's also possible to actually store "PHP" script inside a database table.
I tried to change the field type to "mediumblob" no success!, if i insert any php code in the textarea, all the text
disapears, and for the html, is just not doing anything.

Any help please,
Many thanks i advance,
Mike

Re: Insert and retrieved html and css formated text in Datab

Posted: Thu Dec 08, 2011 3:52 pm
by social_experiment
MikeSpider wrote:Where a title, a text and a picture can be inserted.
When you say "My CMS" is it one you created? I think it's common practice for a CMS to have a field for each of the options and then style them on display and not to store them "styled".

Code: Select all

<?php
 // data retrieved, ready to echo
 $title = '<h1>' . $row['title'] . '</h1>';
 $text = '<p>' . $row['text']. '</p>';
 $picture = '<img src="' . $row['picture'] . '" />';
?>
Have you tried using TEXT type for the field? If you want to allow certain tags within the body of your text field, use strip_tags() which lets you specify which HTML tags to allow and removes all others. To display code you could use <code> tags and style them using CSS
http://php.net/manual/en/function.strip-tags.php