【发布时间】:2016-09-06 07:10:33
【问题描述】:
我为每个在我的网站上拥有个人资料的用户创建了一个图库页面。我希望画廊从数据库中提取图像并在适当的地方使用它们 - 我有这个工作。但是当没有要显示的图像时,我想显示五个带有“即将推出的图像”占位符的块。
如果 foreach 数组为空,如何调整以下 PHP 代码以显示占位符?目前,如果数据库中不存在图像,则画廊空间中不会显示任何内容。
<?php foreach($galleryphotos as $galleryphoto): ?>
<?php if(!empty($galleryphoto['galleryphoto_file'])){ ?>
<div class="csmx-gallery-item csmx-isotope-item col-xs-12 col-sm-6 grid-sizer post-324 portfolio type-portfolio status-publish has-post-thumbnail hentry portfolio-category-france" id="gallery-1928">
<figure>
<a class="csmx-media " href="<?php echo $SITE_URL; ?>media/gallery-photos/<?php echo htmlentities($galleryphoto['galleryphoto_file'], ENT_QUOTES, 'UTF-8'); ?>" title="Sarah">
<img src="<?php echo $SITE_URL; ?>media/gallery-photos/<?php echo htmlentities($galleryphoto['galleryphoto_file'], ENT_QUOTES, 'UTF-8'); ?>" alt="Sarah">
<span class="overlay-background"></span>
<div class="overlay-caption">
<i class="fa fa-picture-o" aria-hidden="true"></i>
<!--<h3>Sarah</h3>-->
<!--<p>Contrary to popular belief, Lorem Ipsum is not simply random...</p>-->
</div>
</a>
</figure>
</div>
<?php } ?>
<?php endforeach; ?>
Marco 提供的包含 IF ELSE 语句的代码;
<?php foreach($galleryphotos as $galleryphoto): ?>
<?php if(!empty($galleryescortphoto['galleryphoto_file'])){ ?>
<div class="csmx-gallery-item csmx-isotope-item col-xs-12 col-sm-6 grid-sizer post-324 portfolio type-portfolio status-publish has-post-thumbnail hentry portfolio-category-france" id="gallery-1928">
<figure>
<a class="csmx-media " href="<?php echo $SITE_URL; ?>media/gallery-photos/<?php echo htmlentities($galleryphoto['galleryphoto_file'], ENT_QUOTES, 'UTF-8'); ?>" title="Sarah">
<img src="<?php echo $SITE_URL; ?>media/gallery-photos/<?php echo htmlentities($galleryphoto['galleryphoto_file'], ENT_QUOTES, 'UTF-8'); ?>" alt="Sarah">
<span class="overlay-background"></span>
<div class="overlay-caption">
<i class="fa fa-picture-o" aria-hidden="true"></i>
<!--<h3>Sarah</h3>-->
<!--<p>Contrary to popular belief, Lorem Ipsum is not simply random...</p>-->
</div>
</a>
</figure>
</div>
<?php } else { ?>
<!-- Add your placeholder image -->
<div class="csmx-gallery-item csmx-isotope-item col-xs-12 col-sm-6 grid-sizer post-324 portfolio type-portfolio status-publish has-post-thumbnail hentry portfolio-category-france" id="gallery-1928">
<figure>
<a class="csmx-media " href="" title="Sarah">
<img src="<?php echo $SITE_URL; ?>media/gallery-photos/no-photo.png" alt="Sarah">
<span class="overlay-background"></span>
<div class="overlay-caption">
<i class="fa fa-picture-o" aria-hidden="true"></i>
<!--<h3>Sarah</h3>-->
<!--<p>Contrary to popular belief, Lorem Ipsum is not simply random...</p>-->
</div>
</a>
</figure>
</div>
<?php } ?>
<?php endforeach; ?>
使用来自 Kiran 的示例更新代码 - 这不会显示存储的图像,而是显示占位符,即使图像值存在;
<?php if(empty($galleryphotos)) $galleryphotos[0]['galleryphoto_file'] = "image-not-available.jpg";
foreach($galleryphotos as $galleryphoto): ?>
<div class="csmx-gallery-item csmx-isotope-item col-xs-12 col-sm-6 grid-sizer post-324 portfolio type-portfolio status-publish has-post-thumbnail hentry portfolio-category-france" id="gallery-1928">
<figure>
<a class="csmx-media " href="<?php echo $SITE_URL; ?>media/gallery-photos/<?php echo htmlentities($galleryphoto['galleryphoto_file'], ENT_QUOTES, 'UTF-8'); ?>" title="Sarah">
<img src="<?php echo $SITE_URL; ?>media/gallery-photos/<?php echo htmlentities($galleryphoto['galleryphoto_file'], ENT_QUOTES, 'UTF-8'); ?>" alt="Sarah">
<span class="overlay-background"></span>
<div class="overlay-caption">
<i class="fa fa-picture-o" aria-hidden="true"></i>
<!--<h3>Sarah</h3>-->
<!--<p>Contrary to popular belief, Lorem Ipsum is not simply random...</p>-->
</div>
</a>
</figure>
</div>
<?php endforeach; ?>
【问题讨论】:
-
如果没有,请尝试
-
@VishnuBhadoriya 谢谢 - 我尝试了 if else 语句,但怀疑我做错了 - 我不知道如何保持 html 原样并围绕它构建 if else 而不是回显找出我在上面的 sn-p 中已有的内容。
标签: php mysql arrays if-statement