Detect "empty" photo value

6 posts by 4 authors in: Forums > CMS Builder
Last Post: November 17, 2009   (RSS)

By rcrofoot - November 16, 2009

Hi (Dave?)-

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

Hi Rick!

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. :)
Donna

--
support@interactivetools.com

Re: [Donna] Detect "empty" photo value

By rcrofoot - November 16, 2009

Thanks Donna...That worked beautifully!

Rick

Re: [Donna] Detect "empty" photo value

By rcrofoot - November 17, 2009

Hi Donna-

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 Dave - November 17, 2009

>I should have asked yesterday why your solution worked and mine didn't.

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.
Dave Edis - Senior Developer
interactivetools.com