Display placeholder image if no image uploaded, on list view

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 21, 2010   (RSS)

By (Deleted User) - April 21, 2010 - edited: April 21, 2010

Hi,

Just wondering if there was a better way to do this.

Pretty standard lister, listing all the properties meeting search criteria.

Properties can have 0-5 pictures uploaded. If 0 pictures loaded, display a placeholder image

<?php $hasimage = 0 ?>
<?php foreach ($record['photographs'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>"
alt="" width="<?php echo $upload['thumbWidth'] ?>"
height="<?php echo $upload['thumbHeight'] ?>" border="0" /></a><br/>
<?php $hasimage = 1 ?>
<?php break ?>
<?php endif ?>
<?php endforeach ?>
<?php if ($hasimage == 0): ?>
<a href="<?php echo $record['_link'] ?>"><img src="/assets/th-no-image.png" width="85" height="60" border="0"></a>
<?php endif ?>


This works, just wondering, is there a better (more elegant) way to do it.

I tried using sizeof(), but if there is no image loaded, it just gives an error.

Re: [AlanMacDonald] Display placeholder image if no image uploaded, on list view

By Jason - April 21, 2010

Hi,

It's not a bad solution. You could trim it down a bit like this:

<?php foreach ($record['photographs'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="<?php echo $record['_link'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>"
alt="" width="<?php echo $upload['thumbWidth'] ?>"
height="<?php echo $upload['thumbHeight'] ?>" border="0" /></a><br/>

<?php break ?>
<?php endif ?>
<?php endforeach ?>
<?php if (!$record['photographs'): ?>
<a href="<?php echo $record['_link'] ?>"><img src="/assets/th-no-image.png" width="85" height="60" border="0"></a>
<?php endif ?>


The other thing I noticed is you're using <?php break ?>. If there are images uploaded, only the first one will be displayed as the break command will end the foreach loop. If you want to display all of the images that have been uploaded, just remove that line.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/