开发者

Display link if image is empty

<div class="actions">
 <?php
    foreach ($images as $image):

        if ($image['Image']['img_file'] != null) {
            echo $html->image('uploads' . DS . 'images' . DS . $image['Image']['img_file'], a开发者_StackOverflow社区rray('alt' => 'Gallery Image'));
            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']));
        }

        else {
            echo $this->Html->link(__('Add image', true), array('action' => 'add'));
        }
        endforeach;
        ?>
</div>

Hi, I wish to achive the following things: if the image is null, I want to show the link 'Add Image', else if there is image, I want to show the link 'Delete'. Using the above code, it does not seem to work. Please assist. Thanks.


If I understood correctly, when there are no images in the gallery you want to show an "add image" link. But when there are no images the array is empty so that loop never runs once. Maybe something like this:

if( !empty( $images ) ) {
    foreach ($images as $image) {
        echo $this->Html->image('uploads' . DS . 'images' . DS . $image['Image']['img_file'], array('alt' => 'Gallery Image'));
        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']));
    }
}
else {
    echo $this->Html->link(__('Add image', true), array('action' => 'add'));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜