简体   繁体   中英

Echo data from php in Codeigniter

I have a piece of javascript that will display a template of html input fields and image.

I am trying to echo the value of $images->icon in the image src. However the value is not echo out. How should I go about doing it?

<img class="gc_thumbnail" src="<?php echo base_url('../contentblock_resources_thumb/'.$images->icon);?>" style="padding:5px; border:1px solid #ddd"/>

Javascript template

<script type="text/template" id="imageTemplate">
            <div class="row gc_photo" id="gc_photo_{{id}}_{{filename}}"
            style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
                <div class="col-md-2">
                    <img class="gc_thumbnail" src="<?php echo base_url('../contentblock_resources_thumb/'.$images->icon);?>" style="padding:5px; border:1px solid #ddd"/>
                </div>
            </div>
        </script>
<script type="text/template" id="imageTemplate">
<div class="row gc_photo" id="gc_photo_{{id}}_{{filename}}"
style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
<div class="col-md-2">
 <?php
$image_path = base_url()."contentblock_resources_thumb"; //save folder in varibaale
if ($images->icon=='') //check images exist or not
 {
echo "no image is here";
}
 else
{
 echo " <img  src='$image_path/$images->icon' />";
}
?>
</div>
            </div>
</script>

试试看(将$images->icon放在base_url()函数之外)

<img class="gc_thumbnail" src="<?php echo base_url('../contentblock_resources_thumb').'/'.$images->icon;?>" style="padding:5px; border:1px solid #ddd"/>

I think you just didn't pass the images to the view in your controller. Here's an example of passing values to view:

$data['images']->icon = 'abcd.png';
$this->load->view('yourView', $data);

Then in view you can simply echo the values:

echo $images->icon;

Bear in mind that $image will be available in your view. The key-value pairs in the array are automatically converted to variables in the view. In order to pass value to view you can either use an array on an object. For more info see Adding Dynamic Data to the View in Codeigniter user-guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM