Question

I am new to zend framewrok. I am trying to show an image from index view. Below is my code:

<?php         
echo "Student List";
//$this->headTitle($this->title);
?>
<p><a href="<?php echo $this->url(array('controller'=>'index', 
        'action'=>'add'));?>">Add new student</a></p>
<table>
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Photo</th>
    <th>&nbsp;</th>
</tr>
<?php foreach($this->students as $student) : ?>
<tr>
    <td><?php echo $this->escape($student->name);?></td>
    <td><?php echo $this->escape($student->email);?></td>
    <td><img src='opt/lampp/htdocs/framework/zf-tutorial/project.png' alt='sdfsd'></td>
    <td>
        <a href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'edit', 'id'=>$student->id));?>">Edit</a>
        <a href="<?php echo $this->url(array('controller'=>'index', 
            'action'=>'delete', 'id'=>$student->id));?>">Delete</a>
    </td>
</tr>
<?php endforeach; ?>
</table>

It only show the alt text not the image. Please help me.

Was it helpful?

Solution

If you use the default folder structure try this:

Create an Folder images. Put your image project.png inside of you public folder (public/images/project.png).

In your view Script use the baseUrl Viewhelper:

<img src="<?php echo $this->baseUrl() . '/images/project.png' ?>" alt='sdfsd'/>

OTHER TIPS

If you want to access images folder contents in PHP code than use this

<img src="<?php echo $this->baseUrl()?>/images/project.png" />

and if you want to access images in your javascript than write below code in your layout.phtml

    <script type="text/javascript">
       var config = {baseUrl: '<?php echo $this->baseUrl() ?>'};
   </script>

now you can access images folder contents like this

<img src="'+config.baseUrl+'/images/project.png" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top