Add Default Image to List Output

12 posts by 3 authors in: Forums > CMS Builder
Last Post: January 27, 2021   (RSS)

By Jenna - January 26, 2021

Hi Mark,

In order to check if a logo image exists, and to display a placeholder in the case it doesn't exist, please try the following:

<?php if ($record['logo_image']) : ?>
<!-- boolean check to see if logo_image exists for the record, returns true or false -->

  <?php foreach ($record['logo_image'] as $upload): // Do not need $index => $upload if you don't utilise $index anywhere in your code ?>
     <!-- show the image from the record, using urlPath as the src -->
     <!-- htmlencode all values to prevent JavaScript injections -->
     <img src="<?php echo htmlencode($upload['urlPath']) ?>" width="200px" alt="<?php echo htmlencode($record['title']) ?> Logo" title="<?php echo htmlencode($record['title']) ?> Logo"><br>
  <?php endforeach ?>
        
<?php else: ?>
  <!-- No uploaded image, show placeholder -->
  <img src="https://DOMAIN.com/PLACEHOLDERIMG.jpg" alt="Placeholder Image">

<?php endif ?>

If you need to only check to see if it's got one image uploaded... you can either add a break just one line above the endforeach like seen below:

<?php foreach ($record['logo_image'] as $upload): ?>
     <img src="<?php echo htmlencode($upload['urlPath']) ?>" width="200px" alt="<?php echo htmlencode($record['title']) ?> Logo" title="<?php echo htmlencode($record['title']) ?> Logo"><br>
     <?php break; ?>
<?php endforeach ?>

 Only if you have multiple logo_image files set up will it begin to display more than one image at a time so the above break would be helpful. If that's never a case you would run into, please don't worry about the extra code, it's only a suggestion for implementation.

Please let me know if you have any questions, I'd be more than happy to help.

J

Jenna Cooke - PHP Programmer
interactivetools.com