Frank Vèssia Posted November 8, 2011 Share Posted November 8, 2011 i'm getting this strange behaviour with image getRandom I want to display all images of one field randomly. So i first count my total images and than use getRandom. <?php $tot = count($gallery->images); $gallery = $gallery->images->getRandom($tot); foreach ($gallery as $image){ ... } I always get the same order but if i change the $tot variable with a smaller value (smaller than the total count) it works... Link to comment Share on other sites More sharing options...
apeisa Posted November 8, 2011 Share Posted November 8, 2011 You could use shuffle method instead. Link to comment Share on other sites More sharing options...
apeisa Posted November 8, 2011 Share Posted November 8, 2011 Sorry for short posts (mobile). Shuffle ia documented here: http://processwire.com/api/arrays/ Link to comment Share on other sites More sharing options...
Frank Vèssia Posted November 8, 2011 Author Share Posted November 8, 2011 Thanks Apeisa. It works fine...i didn't know about the shuffle method. Link to comment Share on other sites More sharing options...
ryan Posted November 8, 2011 Share Posted November 8, 2011 The behavior of PHP's array_rand() function (used by getRandom) does seem a little strange when pulling the entire array. I agree with Apeisa that shuffle() is a better way to go for randomizing the entire thing, whereas getRandom is better for pulling 1 or a few elements. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now