使用cakephp显示单个图像

时间:2011-08-20 08:35:27

标签: php cakephp

<?php
if (!empty($images)) {
    foreach ($images as $image):
        echo $html->image('uploads' . DS . 'images' . DS . $image['Image']['img_file'], array('alt' => 'Gallery Image'));
    ?>
<br/>
<?php
        echo $this->Html->link(__('Delete', true), array('action' => 'delete', $image['Image']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $image['Image']['id']));
    endforeach;
}else {
    echo $this->Html->link(__('Add Profile Image', true), array('action' => 'add'));
}
?>

您好如何在不使用foreach循环的情况下仅显示数据库中的单个图像。例如,我在数据库“default.jpg”中有一个图像。如果用户未添加任何图像,将显示默认图像。然后,如果用户选择添加配置文件图像,则默认图像将替换为用户选择的新图像。然后,如果用户删除了他们的个人资料图像,将显示默认图像。

1 个答案:

答案 0 :(得分:0)

  <?php
  if (!empty($images)) {
   foreach ($images as $image):
       echo $html->image('uploads' . DS . 'images' . DS . $image['Image']['img_file'],    array('alt' => 'Gallery Image'));
?>
<br/>
<?php
    echo $this->Html->link(__('Delete', true), array('action' => 'delete', $image['Image']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $image['Image']['id']));
endforeach;
}else {
 echo $html->image('uploads' . DS . 'images' . DS . 'default.jpg',    array('alt' => 'Gallery Image'));
 echo "<br/>";
echo $this->Html->link(__('Add Profile Image', true), array('action' => 'add'));
}
?>