SHOWING ONLY THE FIRST IMAGE OR GROUP OF IMAGES ON A PAGE - Oct 13th, 2011


User Perchpole asked: On a List page, how do I limit the number of (image) uploads displayed with each record to one?

Dave answered:

If you want to show just one image you can use <?PHP break ?> like this to stop after just one:



<?PHP foreach ($record['gallery_images'] as $upload): ?>
<img src="<?PHP echo $upload['thumbUrlPath'] ?>" width="<?PHP echo $upload['thumbWidth'] ?>" height="<?PHP echo
$upload['thumbHeight'] ?>" alt="" />
<?PHP break ?>
<?PHP endforeach ?>



Jason Sauchuk from Interactive Tools suggested that another option would be to limit your query to only one result and
then put the first record in it's own variable.

For example:



// load records
list($gallery_imagesRecords, $gallery_imagesMetaData) = getRecords(array(
'tableName' => 'gallery_images',
'limit' => 1,
));

$gallery_imagesRecord = $gallery_imagesRecords[0]; // get first record.


You can then use the variable $gallery_imageRecord to show various fields in the first record without needing a foreach
loop.




A variation on this is the ability to show a specified number of images from a series of records before a <?PHP break ?>


Jason Sauchuk offered the following approach:



<?php $count = 0; ?>

<?php foreach ($record['gallery_images'] as $upload): ?>

<img src="<?PHP echo $upload['thumbUrlPath'] ?>" width="<?PHP echo $upload['thumbWidth'] ?>" height="<?PHP echo
$upload['thumbHeight'] ?>" alt="" />

<?php if (++$count > 3) { break; } ?>

<?php endforeach ?>


To randomize the images shown:



<?php $count = 0; ?>

<?php shuffle($record['gallery_images']); ?>

<?php foreach ($record['gallery_images'] as $upload): ?>

<img src="<?PHP echo $upload['thumbUrlPath'] ?>" width="<?PHP echo $upload['thumbWidth'] ?>" height="<?PHP echo
$upload['thumbHeight'] ?>" alt="" />

<?php if (++$count > 3) { break; } ?>

<?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