ProjectAb
ProjectAb

Reputation: 65

Apply CSS Styling to MySQL data fetching with PHP

I am displaying description with

<p> <?php echo $description ?> </p>

Description is stored in database and i want to display description something like this.

Lorem Ipsum is simply dummy text of the printing and typesetting Lorem Ipsum is simply dummy text of the printing and typesetting

● Shape: Round

● Certified 0.50.

● Genuine Natural

But Currently it is displaying like this

Lorem Ipsum is simply dummy text of the printing and typesetting Lorem Ipsum is simply dummy text of the printing and typesetting ?Shape: Round ?Certified 1.00. ?Genuine Natural Earth Diamond

Upvotes: 0

Views: 562

Answers (3)

Blob
Blob

Reputation: 146

While you could put HTML tags into your database, I think that is in principle poor practice. What if sometime later you want to use the same database to produce a PDF? Then you would have to write code getting rid of the HTML tags that are in the DB. The general idea is to keep information separate from the formatting of information.

Instead, you could make a new table, Description_detail, that has a foreign key to Description. In the above example, you would have one Description record, containing the "Lorem ipsem..." paragraph, and three Description_detail records, containing "Shape: round", etc. Then in PHP, print the Description with whatever CSS style you want, and then loop over the matching Description_detail's and print each of them with whatever CSS style you want.

Of course you have to decide project by project when it is worth it to put in extra effort to make a solution that is better "in principle".

"In principle, principle and practice are the same; but in practice, they always differ."

Upvotes: 1

Andrew
Andrew

Reputation: 20111

Use str_replace, something like this:

<p> <?php echo str_replace('?', '<br/> ●', $description); ?> </p>

http://php.net/str_replace

Upvotes: 1

KAD
KAD

Reputation: 11122

I bet your description column type is varchar...

I think the best way to do this is to make the column type text in the database and save any format of HTML you want, in this way It will still be formatted once loaded and view the way you desire.

You can use Dynamic HTML editors to input the formatted HTML or you can just build the tagging yourself through PHP.

One of the good DHTML editors is summernote.

Upvotes: 1

Related Questions