1

I'm stuck trying to echo my product descriptions correctly for my eCommerce store.

Is it possible to write formatted HTML (i.e. with elements and tags... to youtube links, etc) as text in a MySQL description field and then echo it with PHP correctly formatted?

At the moment I am using...

<?php

  echo "<p>".Helper::encodeHTML($product['description'])."</p>";

?>

...but as I say it doesn't output as formatted.

Thanks for the help! :)


Edit; Fixed for the moment ('less anything changes) with...

echo html_entity_decode($var['string']);

i.e.

echo html_entity_decode($product['description2']);

http://uk1.php.net/manual/en/function.html-entity-decode.php

Ashley Smith
  • 113
  • 2
  • 8
  • 1
    what framework or library is this `Helper::encodeHTML` from? – developerwjk Jul 27 '15 at 23:00
  • if you trying to echo $product['description'] it is not good? without Helper::encodeHTML – Pierre-Luc Bolduc Jul 27 '15 at 23:05
  • It seems like the 'Helper::encodeHTML' is removing the html tags. As you can just save it with html tags and echo it. – Nytrix Jul 27 '15 at 23:08
  • I'm not sure. Sorry, I'm such a newbie. I just checked out another post... http://stackoverflow.com/questions/8381651/php-echo-working-html-code-from-mysql ...can I do something like that, but echo $product['description]? I'm using PHPMyAdmin and the description field is set to text. Thanks! :) Yes, the tags are posted but not formatted correctly as HTML markup. – Ashley Smith Jul 27 '15 at 23:08
  • Actually it might be working ok. I think I need to add a second field called description2 and add HTML there instead, as I'm echo'ing this in two places... one using Helper::shortenString to shorten the description, and then parse the full description on the product page. – Ashley Smith Jul 27 '15 at 23:13
  • If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs: echo <<<_EOI_ –  Jul 27 '15 at 23:15
  • Optimmus, I'm not sure what that is I'm afraid. If I try htmlentities for example below, I just get the tags...

    Contents: -

    – Ashley Smith Jul 27 '15 at 23:23
  • Just to confirm... Only you (site owner) have the ability to store said HTML in the database correct?... If not you are exposing yourself to XSS attacks. – scunliffe Jul 27 '15 at 23:29
  • If possible I'd like to keep the same HTML syntax rather than learning to write Heredocs such as $foo and \x41 for a capital A. Yikes! Reading a bit of this looks confusing... http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc – Ashley Smith Jul 27 '15 at 23:30

1 Answers1

0

How about removing the p tag and echo it as is?

    <?php
    echo Helper::encodeHTML($product['description']);
    ?>

or echo it with htmlentities() like this:

    <?php
      echo htmlentities($product['description']);
    ?>

or echo it directly like this:

    <?php
      echo $product['description'];
    ?>
Romnick Susa
  • 1,279
  • 13
  • 31