如果图像为空,则显示链接

时间:2011-08-20 08:06:39

标签: php cakephp

<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'], 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'));
        }
        endforeach;
        ?>
</div>

嗨,我希望得到以下内容:如果图片为空,我想显示“添加图片”链接,否则如果有图片,我想显示“删除”链接。使用上面的代码,它似乎不起作用。请协助。谢谢。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,当图库中没有图像时,您想要显示“添加图像”链接。但是当没有图像时,数组是空的,因此循环永远不会运行一次。也许是这样的:

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