GPPK GPPK - 1 year ago 87
HTML Question

Echo an image tag with site_url() inside PHP tags

I have a loop in my view that outputs all the content gathered from the database:

<?php foreach($content as $contentRow): ?>
echo $contentRow->value;
<?php endforeach; ?>

This works fine for HTML strings like:

<h2><strong>Example Text</strong></h2>

however I have some image content that I would like to display and I have tried the following database entries to no avail:

<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"

I feel like I am missing a step on how to use PHP values in this way.

How do I access the URL of the image and use that to show the image?

Full Code Edit

$CI =& get_instance();

<div class="container">

<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>

<!--//<table class="" id="">-->

<?php foreach($content as $contentRow): ?>
echo $contentRow->value;
<?php endforeach; ?>


<div class="col-md-2"></div>


</div><!-- /.container -->

and the values are being read out in

Answer Source

I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.

Update: To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download