gpt4 book ai didi

php - 如果图像数组为空,则显示替代图像

转载 作者:行者123 更新时间:2023-11-28 23:25:57 25 4
gpt4 key购买 nike

我为每个在我的网站上有个人资料的用户创建了一个画廊页面。我希望画廊从数据库中提取图像并在适当的地方使用它们 - 我有这个工作。但是,当没有要显示的图像时,我想显示五个带有“即将显示图像”占位符的 block 。

如果 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; ?>

最佳答案

我想您想在图库为空时显示占位符照片。如果是这样,那么您可能想这样做:

<?php if(empty($galleryphotos)) $galleryphotos[0]['galleryphoto_file'] = "no-photo.png";
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; ?>

上述代码的变化:

  1. 第一行检查 $galleryphotos 数组是否为空,如果为真,则向其添加一个占位符。
  2. 删除 if 条件,因为它是不必要的
  3. 其余代码与它应该工作的一样。

更新#1:

问题可能是由于第一个 if 子句尝试将条件更改为:

if(count($galleryphotos)==0) $gallerypho...

关于php - 如果图像数组为空,则显示替代图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39342676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com