php echo working html code from mysql? - php

I have a chunk of html code stored in a field in a mysql database. If i try and echo it out it just sits on the page as text. Is there a way to echo it out in a working fashion. ( Meaning the html displays what it is meant to display )
The code is stored in a varchar field and i used nl2br with special characters to insert it. The code display's as text but it is what it should be.
Again can i echo the code out in a working way?
EDIT - This is the code that is displayed on the page.
<img name="" src="http://www.mysite.com/assets/viewcart.png" width="114" height="48"alt="" />
and this is the code stored in the database.
<img name="" src="http://www.mysite.com/assets/viewcart.png" width="114" height="48" alt="" />
The Echo request i used to get the code out was a simple.
echo $row['htmlcode'];
Thanks for any help and examples.

You could try running your text through html_entity_decode() before echoing it.
as in echo html_entity_decode($row['htmlcode']);

Related

Is it possible to echo text stored in DB that has PHP echo statements in it

So a bit of a strange question, within my mysql database I have a column which holds text. Within that text field HTML code, plain text and several PHP echo statments.
When I echo out the contents of that column, the text and HTML comes out fine and is layed out correctly but the echo statements are just text.
For example, in a DB column is stored this:
Some text some text <b>some html</b> some text some text
<img src="../Images/Blog/<?php echo($blogimage);?>" class="imagetext-wrap" alt="<?php echo($blogtitle);?>">
Now that echo's out as
Some text some text **some html** some text some text
<?php echo($blogimage);?> <?php echo($blogtitle);?>
The variables $blogimage and $blogtitle already hold the values from the other columns of the DB, is there a way for them to display from the text column field when it is echo'd out?
This is quite hard to describe so I hope you get what I am asking
its like this if you question
echo "<img src="../Images/Blog/'.$blogimage.' " class="imagetext-wrap" ">

Echo a XHTML tag

When I try to echo most of XTHML tags inside a PHP loop which one echo's data from a MySQL it just shows text in browser. I'm running XAMPP Control Panel with Apache and MySQL for tests.
CODE:
echo "<textarea readonly>";
while ($wiersz = mysql_fetch_array($sql_wynik_zapytania))
{
echo " ".$wiersz['NICK'].":\n";
echo $wiersz['KOMENT']."\n"."<hr />"."\n";
}
echo "</textarea>";
The results in browser:
NICK:
a
<hr />
NICK:
asdaa
<hr />
pallluch:
cccc
"a" "asdaa" "cccc" are just random texts added to table in databse for tests.
Echo works perfectly fine with <textarea>, but <hr> doesn't seems to.
Can anyone help?
A textarea element can't contain anything other than plain text.
You are writing invalid HTML and the browser is trying to recover by treating the < character as plain text instead of the start of a tag.
It looks like you aren't using the textarea to accept user input anyway (you've made it readonly). Don't use a textarea element here. You probably want to use <pre> instead.

Including original text format

I would like for posts returned from my database, from users, to be formatted the same way they originally input them.
Like when I echo out the text from a row of data it comes out like this:
Hello There.
When the user originally formatted it like this:
Hello
There.
Notice the return? How do I achieve this?
Here is my html:
<form method="post" action="share.php">
<textarea name="story"></textarea>
<div id="share-something-bottom">
<div id="share-something-camera">
<img src="images/camera.png"/>
<button type="submit" class="share"
name="share"><p>Share</p></button>
</div>
</div>
</form>
I am using a varchar field in my database table.
Thanks
Store the formatting in the DB and make sure you escape it before storing it.
If you wish to preserve newlines when you echo them on the page, you need to use nl2br().
echo nl2br($string_from_database);
You could also wrap the string in <pre> tag, but that will also affect the used font (by default).

php to display text as html formated stored in database

I have one text saved in MySQL Database
<p> Celebrate with these amazing<br /> offers direct from the
Now when I print that text using
echo
I got this output
<p> Celebrate with these amazing<br /> offers direct from the
but U want to display that as
Celebrate with these amazing
offers direct from the
like HTML print.
when i see in db its stored like bellow
<p>
Celebrate with these amazing<br />
offers from
How to do this?
Assuming your database has saved the escaped information like this:
<p>
Celebrate with these amazing<br />
offers from
Then you could just use PHP's html_entity_decode function to output that block of HTML.
maybe this could help you http://php.about.com/od/phpwithmysql/qt/php_in_mysql.htm
If you are outputting to the console (it's not clear from your question what you are trying to output to):
If you look at the PHP strip_tags documentation, you should be able to reach something approximating what you want. http://uk3.php.net/manual/en/function.strip-tags.php
With strip_tags(), you can allow the tag to remain and then replace it with "\n" afterwards.
Otherwise, if you are outputting to browser, then other posts on this page have the answer - you must be storing the htmlentities() version of the data in the database.

Generating a random number and adding to an HTML argument's string

I'm trying to make a header image change randomly based on a random number being chosen. This is the code I have right now, and it requires the entire tag.
<img src="http://www.example.com/site_gfx/headers/header_<?php echo(rand(1,7)); ?>.png" width="980" height="230" alt="Example Site" />
Is there any reason it would be dying like it is? Where the <?php echo part is is the PHP code i'm using to generate the random number, and I'd like to include that into the string for the img src
How's it dying? I'd try print or echo without parentheses, as I haven't seen echo() used before:
print():
<?php print(rand(1,7)); ?>
echo:
<?php echo rand(1,7); ?>
Figured it out. The problem is I didn't realize the <?php area was running within an echo command (stupid little oversight on my part). But modifying the echo statement that I was working with fixed it.
Thanks for the pointers, everyone.
Any reason you want to do this with PHP, it would probably be easier with javascript. try to remove the rand(1,7) from the bracket so as to have:
echo rand(1,7)
This would be a better approach to doing this
<?php
$number = rand(1,7);
echo '<img src="http://www.example.com/site_gfx/headers/header_' . $number . 'png" width="980" height="230" alt="Example Site" />'
?>
There may be an issue with using the <?php tag inside of another tag.

Categories