Detect "empty" photo value
6 posts by 4 authors in: Forums > CMS Builder
Last Post: November 17, 2009 (RSS)
Trying to display a photo when one exists, and a generic photo when one doesn't exist using the following code:
<?php foreach ($record['photos'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="250px" height="171px" border="0" alt='' /><br/>
<?php else: ?>
<img src="images/photoscomingsoon.jpg" width="250px" height="171px" border="0" alt='' /><br/>
<?php endif ?>
<?php break; //only interestes in 1st photo in loop?>
<?php endforeach ?>
Do you see anything wrong here...It doesn't work...The line after "if" fires correctly and displays a photo when one exists. The line containing "images/photoscomingsoon.jpg" never fires, and I know for a fact there are some records without photos, so shouldn't the "images/photoscomingsoon.jpg" execute?
Thanks for the help...Rick
Re: [rcrofoot] Detect "empty" photo value
By Donna - November 16, 2009
Try something like this:
<?php $upload = @$record['photos'][0] ?>
<?php if ($upload): ?>
show upload
<?php endif ?>
<?php if (!$upload): ?>
show no upload image
<?php endif ?>
Give that a try and let me know how it works. :)
--
support@interactivetools.com
Re: [Donna] Detect "empty" photo value
Rick
Re: [Donna] Detect "empty" photo value
Thanks again for the solution. I should have asked yesterday why your solution worked and mine didn't.
Here's yours:
[font "Verdana"]<?php $upload = @$record['photos'][0] ?>
<?php if ($upload): ?>
Here's mine:
[font "Verdana"]<?php foreach ($record['photos'] as $upload): ?>
<?php if ($upload['isImage']): ?>
Does it have something to do with NULL vs. EMPTY values...
Thanks, Rick
Re: [rcrofoot] Detect "empty" photo value
By Damon - November 17, 2009
There is a number of ways to code this up. This is the way I usually do it:
<?php foreach ($gallery['photos'] as $photos): ?>
<?php if ($photos['isImage']): ?>
...insert code here to display image..
<?php endif; ?>
<?php endforeach ?>
<?php if (!$gallery['photos']): ?>
No images available.
<?php endif; ?>
Does that help?
Damon Edis - interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [rcrofoot] Detect "empty" photo value
By Dave - November 17, 2009
It's because the foreach loop loops over all the images, but if there are no images it doesn't loop at all and no code inside it gets run.
So your code that displays "Sorry no images" needs to be outside the foreach loop.
Let me know if that makes sense.
interactivetools.com