2011-08-20 58 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')); 
} 
?>