INSERTING RANDOM ROTATING IMAGES ON A WEB PAGE FROM RANDOM RECORDS - May 19th, 2013


User SkidderChains wanted to provide images for a rotating image header with an interesting twist. The images had to be
pulled from a random record in a multi-record editor and then from a random image in that record’s image upload field.

Greg Thomas from Interactive Tools offered this solution:


// load records from 'gallery_2'
list($gallery, $gallery_2MetaData) = getRecords(array(
'tableName' => 'gallery_2',
'orderBy' => 'RAND()',
'loadUploads' => true,
'allowSearch' => false,
));

//shuffle all of the images for each record
foreach($gallery as $images){
shuffle($images['image']);
}

//counter
$n = 0;
//max number of images you want to pull from each section;
$max = 5;
while($n <= $max): ?>

<?php foreach($gallery as $key => $images): ?>

<?php //If there is a picture in the image array with a key of $n, assign it to the $image varible, else return
false
if($image = @$images['image'][$n]): ?>

<img src="<?php echo $image['urlPath']; ?>" alt="<?php echo $image['info1']; ?>" />
<?php endif; ?>

<?php endforeach; ?>
<?php $n++; ?>
<?php endwhile; ?>


Greg said:

This is just an example, and you will have to modify the code to work with your setup.

“I'm using the getRecords function to randomly sort the sections, then cycling through them and displaying each
individual image using the $n variable to select a single image from the image array. So first it will display all of
the images from each section with a key of 0, then 1, 2 etc.



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