0

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

7
  • Will it always have the question marks and the list starts after the first question mark ? Commented Jul 25, 2015 at 17:25
  • Print it differently, how is this stored in your database? Can you retrieve the list items separately? Commented Jul 25, 2015 at 17:25
  • Description is stored in one column. Commented Jul 25, 2015 at 17:26
  • 1
    Then you're stuck with parsing the string. If you can rely on the question marks preceding each item, then find and replace i guess. You need to change the html output. Commented Jul 25, 2015 at 17:26
  • Ok find and replace how to apply new line after paragraph ends and li starts ? Commented Jul 25, 2015 at 17:28

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Why can't varchar hold HTML?
It can hold html. but its max size is 255 bytes, while text can hold up to 65,532 bytes. Html will take a lot of bytes space... But both can hold markup.
I think varchar also holds HTML because i inserted html tags in varchar field and it is still working
Depends on the version of mysql. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. dev.mysql.com/doc/refman/5.0/en/char.html So text and varchar both should function the same with less than 65k of data...
Good point @chris85 concerning the size part, thanks for stating that.
1

Use str_replace, something like this:

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

http://php.net/str_replace

1 Comment

Thanks for answering but converting type from varchar to text works
1

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."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.