Image Shuffle and Randomising - only randomising the first 4 images

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 7, 2013   (RSS)

By benedict - August 5, 2013

Hi, I'm using the code you posted here at:

http://www.interactivetools.com/forum/forum-posts.php?postNum=2218577#post2218577

It works with my multirecord Gallery section which has 4 records in it with an upload field holding approx. 25 images per  record/gallery - Weddings/Corporate/NYE/Private Charters

When I say it works, it gathers up the images and presents them in random order... almost.

What it does is it randomises only the presentation of the first 4 images - it is still the same 4 first images everytime (the first in each record of Weddings/Corporate/NYE/Private Charters). I want those images to be completely shuffled every time the page loads.

You can see it working down the bottom of the page in the carousel at http://vicstar.stockstreetserver.com/index.html - note when you refresh, it is showing the same 1st image each time, not randomising the whole array.

My modified code of yours is:

<div class="carousel">
            <div class="carousel-center">
                <div class="gmask-center">
                    <div class="gmask">
                        <ul>
                        <?php
  
// load records from 'galleries'
list($gallery, $galleriesMetaData) = getRecords(array(
'tableName' => 'galleries',
'orderBy' => 'RAND()',
'loadUploads' => true,
'allowSearch' => false,
));

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

//counter
$n = 0;
//max number of images you want to pull from each section;
$max = 7;
while($n <= $max): ?>
                        <?php foreach($gallery as $key => $images): ?>
                            <li>
                                <div class="photo">
                                    <?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['images'][$n]): ?>
<?php shuffle($images['images']); ?>
                                <img title="<?php echo $image['info1']; ?>" alt="<?php echo $image['info1']; ?>" src="<?php echo $image['thumbUrlPath3']; ?>" width="309" height="206"/>
                                    <?php endif; ?>
                                </div>
                            </li>
                            <?php endforeach; ?>
<?php $n++; ?>
<?php endwhile; ?>
                        </ul>
                    </div>
                </div>
                <a href="#" class="btn-prev">prev</a>
                <a href="#" class="btn-next">next</a>
            </div>
        </div>

Hi,

You can randomise the order of the galleries as well as its images by shuffling the gallery array as well:

<?php
    
  // load records from 'galleries'
  list($gallery, $galleriesMetaData) = getRecords(array(
    'tableName' => 'galleries',
    'orderBy' => 'RAND()',
    'loadUploads' => true,
    'allowSearch' => false
  ));

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

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

?>

This will shuffle the the items in the gallery array after all of the images in each gallery record has been shuffled.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi benedict, 

The problem is that when you use a foreach loop, you can't edit the compontents inside of it unless you makes changes to the array directly, I think you need to change your foreach loop for the galleries to this:

<?php
    
  // load records from 'galleries'
  list($gallery, $galleriesMetaData) = getRecords(array(
    'tableName' => 'galleries',
    'orderBy' => 'RAND()',
    'loadUploads' => true,
    'allowSearch' => false
  ));

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

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

?>

So now the foreach loop cycles through each element and uses its key to make changes to the item directly on the array.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By benedict - August 7, 2013

Boom. Works. You beauty! Thanks, mate.