COUNTING IMAGES IN A MULTI-IMAGE UPLOAD FIELD - Jan 18th, 2012


I needed to count the number of images that existed in a multi-image upload field that was the only record in my editor,
so that if there was only one image I could display it as an image and if there was more than one image I could display
them as a slide show.

I started out with another recipe in the Cookbook called: LIMITING THE NUMBER OF IMAGES ON A PAGE AND SHOWING CODE IF
EXCEEDED (just above this one)

The basic approach in that recipe is:


<?php foreach ($home_page_slidesRecords as $record): ?>
<?php $count = 0; ?>
<?php foreach ($record['image'] as $upload): ?>
<?php $count++; ?>
<?php endforeach; ?>
<?php echo $count>1 ? "There is more than one thumbnail" : "There is only one thumbnail" ; ?>
<?php endforeach; ?>


I modified the code so that instead of messages, the required code was inserted into the page. I also added a checkbox
field to allow the client to decide if they wanted to use this as a slide show regardless of how many images were
uploaded. I also added <?php shuffle($record['images']) ?> to the code to randomize the image displayed when there was
no slide show.


<?php foreach ($home_page_slidesRecords as $record): ?>
<?php $count = 0; ?>
<?php foreach ($record['image'] as $upload): ?>
<?php $count++; ?>
<?php endforeach; ?>
<?php if ($count > 1 && $home_page_slidesRecord['slideshow'] == 1): ?>
<div id="show" class="slideshow"></div>
<?php else: ?>
<?php shuffle($record['images']) ?><?php foreach ($record['image'] as $upload): ?><img src="<?PHP echo
$upload['thumbUrlPath'] ?>" width="<?PHP echo $upload['thumbWidth'] ?>" height="<?PHP echo $upload['thumbHeight']
?>" alt="" />
<?PHP endforeach ?>
<?php endif ?>
<?php endforeach; ?>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service