shuffle images

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

By SkidderChains - November 19, 2012

Hoping someone can help with this one.....

I have a multi record section named Images that provides the images for a rotating image header.. Within that section, there is a field Image where multiple images can be uploaded.

There is also a check box to select which pages the Record Images should display on.

I am trying to set it up so that the code randomly selects a section, and then the image from that section displays one image. then it moves on to the next random sections and pulls one image....continuously. This is what I have so far, and it works, but only takes the first image it selects. I would like it to go back to the section and skip the image it has already shown and move on to the next image...

<?php shuffle($imagesRecords); ?>

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

<?php if ($record['locations']): ?>

<?php $count = 0; ?>
<?php shuffle($record['image']); ?>
<?php foreach ($record['image'] as $upload):?>
<?php if (++$count > 1) { break; } ?>


<img src="<?php echo $upload['urlPath'] ?>" alt=""/>


<?php endforeach ?>

<?php endif ?>

<?php endforeach ?>


I hope that makes sense. here is the link to the page as it is working now. Randomly pulling a section and then shuffling one image from each section.

http://new.wallingfords.com/locations.php

THanks.

Re: [SkidderChains] shuffle images

By gregThomas - November 19, 2012

Hi SkidderChains,

I've come up with something that should do what you want:

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


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

So 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.

Let me know if this isn't what your after.

Cheers
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] shuffle images

By SkidderChains - November 21, 2012

It seems to be working, but taking a long time to load the images and is effecting the transition.... Any thoughts. I will have to trouble shoot on this end.

Thanks for the help....