Ben Ben - 2 months ago 7
MySQL Question

Showing alternative images if image array is empty

I have a gallery page for each user who has a profile on my site. I want the gallery to pull images from the database and use them in the appropriate places - I have this working. But when there are no images to display I want to display five blocks with an 'images coming soon' placeholder.

How can I adapt the following PHP code to show the placeholders if the foreach array is empty? At present if no images exist in the DB nothing is shown in the gallery space.

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


Code including IF ELSE statement as provided by Marco;

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


Updated code with example from Kiran - this doesn't display the stored images but rather displays the placeholder, even if image values exist;

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

Answer

I think you wanted to show a placeholder photo when a gallery is empty. If so, then you might wanna do this:

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

Changes in above code:

  1. first line checks if the $galleryphotos array is empty and if it is true, adds a placeholder to it.
  2. removed if condition as it will be unnecessary
  3. rest code is same as it should work.

Update #1:

The problem might be due to first if clause try changing the condition to:

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