【问题标题】:Showing alternative images if image array is empty如果图像数组为空,则显示替代图像
【发布时间】: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


【解决方案1】:

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

<?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...

【讨论】:

  • 它似乎可以显示无图像占位符,但它在确实有图像的记录上显示无图像占位符,因此数组不为空。
  • 我没有把你带到那里,你能举个例子吗?
  • 你是说占位符到处都显示?
  • 部分方式我认为你做到了,占位符至少出现了,但它显示在每个配置文件中——即使有需要通过的值——我已经用你的方法更新了我的 Q .
  • 是的 - 无处不在,即使有结果要显示,也没有图像显示 - 我只是得到占位符。
【解决方案2】:

试试这个:

<?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 } else { ?>
            <!-- Add your placeholder image -->
            <img src="https://pbs.twimg.com/profile_images/762369348300251136/5Obhonwa.jpg" />
        <?php } ?>
        <?php endforeach; ?>

【讨论】:

  • 感谢@Marco,但它似乎不起作用。我仍然什么也没显示。
  • 但是你需要在 else 语句中放一些东西才能看到占位符
  • @MarcoSantarossa 我已将我的代码与您的建议一起包含在其中 - 您会看到我正在尝试复制相同的 div,但使用的是占位符图像,而不是从数据库中提取的图像。
  • @Ben 你确定图片存在吗?删除 &lt;h3&gt;Sarah&lt;/h3&gt; 的 cmets 以进行调试,因此如果您在语句中输入,您会看到该标记。
  • @MarcoSantarossa 图像肯定存在,我也刚刚在浏览器中浏览到它 - 它加载了。当我取消注释时,标题标签没有出现。这就是我之前认为我做错的事情 - 我认为我需要的代码比添加 &lt;?php } else { ?&gt; 更多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 2022-01-09
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多