Testing for "no image" in a page viewer

4 posts by 2 authors in: Forums > CMS Builder
Last Post: February 21, 2008   (RSS)

By rcrofoot - February 19, 2008

Hi Dave-

Regarding the code below to display an image, I noticed that $upload['isImage'] will return the value of 1 if an image has been uploaded by CMS...I need to test for no image to display an alternate photo, so I thought $upload['isImage'] would return 0 if no pix existed, but it doesn't...Can you point me in the right direction to test for no pix...(the code in red is what I've added, but it never gets executed when there's no image)...

Thanks, Rick



<!-- STEP3: Display Uploads from 'main_photo' (Paste this where you want your uploads displayed) -->
<!-- Upload Program Fields : num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath -->
<!-- Upload Image Fields : isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight -->
<!-- Upload Info Fields : info1, info2, info3, info4, info5 -->
<?php if ($record): ?>
<?php foreach (getUploads($options['tableName'], 'main_photo', $record['num']) as $upload): ?>
<?php if ($upload['isImage']): ?>
<?php echo "upload['isImage']: ".$upload['isImage']; ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" /><br/>

<?php else: ?>
<?php echo 'Add No Main pix here...'; ?><br/>[/#ff0000]

<?php endif ?>
<?php endforeach ?>
<?php endif ?>
<!-- STEP3: /Display Uploads from 'main_photo' -->

Re: [rcrofoot] Testing for "no image" in a page viewer

By Dave - February 20, 2008

Hi Rick,

What we can do is just assign the uploads to a variable first, and then you can test if it's empty of not.

Replace this:
<?php foreach (getUploads($options['tableName'], 'main_photo', $record['num']) as $upload): ?>

With this:
<?php $uploadList = getUploads($options['tableName'], 'main_photo', $record['num']); ?>

<?php if (empty($uploadList)): ?>
No uploads for this record!
<?php endif ?>

<?php foreach ($uploadList as $upload): ?>


Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Testing for "no image" in a page viewer

By rcrofoot - February 20, 2008

Dave-

Your solution worked perfectly...Why didn't I think of that!

So, my understanding is that in the following inner foreach, any iteration will skip what's inside the loop if there's no contents...in this case an image in the 'main_photo' field...i.e. if ($upload['isImage']): won't even test because the 'main_photo' field is empty...

<?php foreach ($listRows as $record): ?>

<?php foreach (getUploads($options['tableName'], 'main_photo', $record['num']) as $upload): ?>
<?php if ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="255" height="170" border="0" /><br/>
<?php endif ?>
<?php endforeach ?>

<?php endforeach ?>

Thanks again for all the help...Rick