2011-08-20 60 views
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 $this->Html->link(__('Add Profile Image', true), array('action' => 'add')); 
} 
?> 

你好我怎么只显示从数据库中的单个图像,而无需使用foreach循环。例如,我在数据库“default.jpg”中有一个图像。如果用户没有添加任何图像,则会显示默认图像。那么如果用户选择添加配置文件图像,则默认图像将被替换为用户选择的新图像。那么如果用户已经删除了他们的个人资料图像,则会显示默认图像。显示单个图像使用CakePHP

+0

不要在网址链接中使用DS! DS用于文件系统。只需使用/ – mark

+0

只需使用?意思是 – dwynae

+0

他的意思是不要在URL中使用''upload.'DS.'images''。改为使用''uploads/images''。 DS可以根据运行服务器的系统进行更改,但URL总是使用'/'。 – JJJ

回答

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')); 
} 
?>