artaylor Posted April 12, 2013 Posted April 12, 2013 Hey all, My client wants a scrolling banner but would like to randomize the images on each page and I am having problems. I have an image field with unlimited images in a site-settings page. I try to shuffle the array returned but they are always in the same order. The code I am using is below. Any and all help is appreciated. function shuffleAssoc($list) { if (!is_array($list)){ return $list; } $keys = array_keys($list); shuffle($keys); $random = array(); foreach ($keys as $key) $random[$key] = $list[$key]; return $random; } function bannerShow() { if(count(wire("pages")->get("/site-settings/")->site_banner_images)) { $bannerimages = wire("pages")->get("/site-settings/")->site_banner_images; $out =""; // randomize the images $randimages = shuffleAssoc($bannerimages); foreach ($randimages as $image) { $out .="<img src='$image->url' />/n"; } echo $out; } else { echo "<p>NO BANNER IMAGES FOUND</p>" ; } }
adrian Posted April 12, 2013 Posted April 12, 2013 I haven't looked over the shuffleAssoc function properly, but you can probably just use PW's shuffle(): $randimages = $bannerimages->shuffle(); 2
artaylor Posted April 12, 2013 Author Posted April 12, 2013 @adrian: that would be far too simple. Looking for a more difficult method. Thanks will go give it a try. Solved Thanks @adrian, it worked like a charm! 2
diogo Posted April 12, 2013 Posted April 12, 2013 In pw, if something is being too complicated you're probably doing it wrong 7
kongondo Posted April 12, 2013 Posted April 12, 2013 What was that again about PW API being jQuery-inspired? Yeah!, Write Less, Do More! 4
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