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

时间:2016-09-06 07:10:34

标签: php mysql arrays if-statement

我为每个在我网站上都有个人资料的用户提供了一个图库页面。我希望画廊从数据库中提取图像并在适当的位置使用它们 - 我有这个工作。但是当没有要显示的图像时,我想要显示五个块,其中即将出现的图像'占位符。

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

2 个答案:

答案 0 :(得分: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. 如果条件没有必要删除
  3. 休息代码与应该工作的相同。
  4. 更新#1:

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

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

答案 1 :(得分:0)

试试这个:

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