Pete Posted July 30, 2011 Share Posted July 30, 2011 Hi folks What I'd like to do is grab a random image to display. It's not as simple as grabbing a random image from a specific page though - I've created a Page Headers page with each child page being a specific category which contains multiple images for that category (Natures, Movies etc.). Therefore what I need to do is pick a random image based on a template - in this case it's called page-headers. This is pseudo-code, but how I'd imagine it working is something like $pages->find("template=page-headers")->images->getRandom(); Is it possible to do it that way or would I have to grab every page, iterate through them building a list of all images and pick one from random from that list? Link to comment Share on other sites More sharing options...
ryan Posted July 30, 2011 Share Posted July 30, 2011 I'm responding from phone so keeping it short: $image = $pages->get("template=page-headers, sort=random")->images->getRandom(); 4 Link to comment Share on other sites More sharing options...
Pete Posted July 30, 2011 Author Share Posted July 30, 2011 Awesome stuff ryan - works a treat Link to comment Share on other sites More sharing options...
onjegolders Posted April 25, 2012 Share Posted April 25, 2012 I've used similar code and got it displaying 6 random images, however I want it to show 6 random images from across all the pages that share the gallery_entry template. At present it's only showing from the same album (page): <?php $images = $pages->get("template=gallery_entry, sort=random")->images->getRandom(6); foreach ($images as $image) { $img = $image->size(80,60); ?> <a href="<?php echo $config->urls->root; ?>albums"><img src="<?php echo $img->url; ?>" width="<?php echo $img->width; ?>" height="<?php echo $img->height; ?>" alt="Latest Photos" /></a> <?php } ?> Any idea on how to get random images from more than one page? Thanks Link to comment Share on other sites More sharing options...
ryan Posted April 25, 2012 Share Posted April 25, 2012 I would suggest grabbing one randomly from each page: $entries = $pages->find("template=gallery_entry, sort=random, images.count>0, limit=6"); $images = array(); foreach($entries as $entry) { $images[] = $entry->images->getRandom(); } foreach($images as $image) { $img = $image->size(80,60); echo "<a href='/albums/'><img src='{$img->url}' alt='Latest photos' /></a>"; } 2 Link to comment Share on other sites More sharing options...
onjegolders Posted April 25, 2012 Share Posted April 25, 2012 Thanks Ryan, is it possible to get all the images that use the template and then randomly choose 6 photos? Link to comment Share on other sites More sharing options...
diogo Posted April 26, 2012 Share Posted April 26, 2012 Have a look here http://processwire.com/talk/topic/868-array-merge-leaves-empty-array-whats-wrong/page__hl__%2Bmerge+%2Barrays I think you will find your answer there (sorry for the short answer, I'm in a hurry) Link to comment Share on other sites More sharing options...
Soma Posted April 26, 2012 Share Posted April 26, 2012 You can use a WireArray and fill it with image objects from pages. Then get a random count from that WireArray. It's also in the cheatsheet. // new WireArray object for storing images $images = new WireArray(); // we find all pages using a specific template that have at least 1 or more images. // assumes the image field is named "myimages". If we deal with really lots of pages/images, consider using a limit to avoid performance problems $pa = $pages->find("myimages.count>0, template=basic-page, sort=random, limit=10"); // loop all found pages and import the images to the $images object foreach($pa as $p){ $images->import($p->myimages); } // output 4 random images from the wire array foreach($images->getRandom(4) as $img){ echo "<img src='{$img->url}'/><br/>"; } Thanks Ryan, is it possible to get all the images that use the template and then randomly choose 6 photos? Reading again, and looking at Ryans code, it does pretty much the same, just little different way. Think about it again... Is there really any difference ? From: get ALL images from ALL pages and choose 6 random from them. To: get randomly 6 pages from ALL pages and choose 1 randomly from each page. I don't see really a difference, only that the second is far better and the way to go, because you limit the find for pages in the first place. Getting all images from all pages could create problems and not so scalable. 3 Link to comment Share on other sites More sharing options...
diogo Posted April 27, 2012 Share Posted April 27, 2012 Soma, it's completely different... if you have 2 pages, one with 100 images and the other with only one, with that method we would get the sole image of the second page 50% of the times... I think what onjegolders wants, it's to have all pictures to be in equal circumstances. Link to comment Share on other sites More sharing options...
onjegolders Posted April 27, 2012 Share Posted April 27, 2012 Soma, it's completely different... if you have 2 pages, one with 100 images and the other with only one, with that method we would get the sole image of the second page 50% of the times... I think what onjegolders wants, it's to have all pictures to be in equal circumstances. Yep that's exactly right Diogo, before I was getting pictures always from the same "album" (page) and that page changed randomly. Your code Soma looks like it could work so thanks guys! Link to comment Share on other sites More sharing options...
Soma Posted April 27, 2012 Share Posted April 27, 2012 Yep that's exactly right Diogo, before I was getting pictures always from the same "album" (page) and that page changed randomly.Your code Soma looks like it could work so thanks guys! Looking at your code, yes. Because ->get() will always get only 1 page and 6 random images from that page. Ryan's example won't do that because it uses ->find() and returns 6 random pages everytime. As I said it's not exactly the same, of course it depends a lot how many pages and images you have. But going with a case of 20+ pages with ~20 images each will work as good. Considering having 10000+ pages with each ~20 images, Ryans example would work still as good, but grabing all pages and images will most likely end in a out of memory. Link to comment Share on other sites More sharing options...
Joss Posted February 18, 2014 Share Posted February 18, 2014 Being that I was sorting through pages and then sorting through a repeater and then sorting through an image field array and then only wanted one and really didn't care which one it was as long as it was one of them and it was possibly different each time or at least as often as no one noticed. I was so glad to find that there is such a thing as getRandom() just now! Happiness can be easily won. Link to comment Share on other sites More sharing options...
mrjasongorman Posted May 13, 2015 Share Posted May 13, 2015 A slight variation, if you want to to randomly sort images on a page, lets say for a slider you can do this: $slider = $page->images->shuffle(); Just used it on a project, works perfect. Link to comment Share on other sites More sharing options...
videokid Posted May 14, 2015 Share Posted May 14, 2015 I'd like to add this simple module: https://gist.github.com/somatonic/5568201, works pretty well 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