showing wrong number of thumbs

5 posts by 2 authors in: Forums > CMS Builder
Last Post: March 31, 2010   (RSS)

By rez - March 30, 2010 - edited: March 30, 2010

Why is this code showing 16 thumbs? I want to display a certain number of random thumbs.



<?php foreach ($galleryRecords as $record): ?>
<?PHP shuffle($record['uploads']) ?>
<?php $counter = 1; ?>
<?php foreach ($record['uploads'] as $upload): ?>

<li><a href="<?php echo $upload['urlPath'] ?>" ><img src="<?php echo $upload['thumbUrlPath'] ?>" /></a></li>

<?php if($counter == 8) : ?><?PHP break ?><?php endif; ?>
<?php $counter++ ?>
<?php endforeach ?>
<?php endforeach ?>

Re: [chris] showing wrong number of thumbs

By rez - March 30, 2010 - edited: March 30, 2010

Oh. That could come in handy, cool.

But yes, I wanted to show random thumbs from all the records. Actually 12.

Re: [rez] showing wrong number of thumbs

By Chris - March 31, 2010

Hi rez,

The trick here is to first combine each record's list of uploads into one big list of all the uploads, then shuffle it.

Also, instead of using the $counter trick, I used array_slice to trim the list down to its first 12 entries. $counter would have worked just as well too. :)

<?php
// make one big list of all the 'uploads' from all the records
$allUploads = array();
foreach ($galleryRecords as $record) {
$allUploads = array_merge($allUploads, $record['uploads']);
}

// randomize their order
shuffle($allUploads);

// keep only the first 12 uploads
$allUploads = array_slice($allUploads, 0, 12);
?>
<?php foreach ($allUploads as $upload): ?>
<li><a href="<?php echo $upload['urlPath'] ?>" ><img src="<?php echo $upload['thumbUrlPath'] ?>" /></a></li>
<?php endforeach ?>


I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] showing wrong number of thumbs

By rez - March 31, 2010

Very useful for a lot of random things. :)