mcgarriers Posted May 13, 2013 Share Posted May 13, 2013 Hi, New to ProcessWire but already thoroughly enjoying developing with it. I have a Home Page and I'd like to show a random image from ANY of my web pages on it. The only thing is it needs to find the image from a child page of the parent with ID '1010'. Can someone explain how I achieve this? Thank you, Michael Link to comment Share on other sites More sharing options...
Wanze Posted May 13, 2013 Share Posted May 13, 2013 Hi michael, Something like this should work (assuming your image field is called 'images'): $img = $pages->get('parent=1010, images.count>0, sort=random')->images->getRandom(); echo "<img src='{$img->url}' alt='{$img->description}'>"; 2 Link to comment Share on other sites More sharing options...
kongondo Posted May 13, 2013 Share Posted May 13, 2013 (edited) Hi Michael. Welcome to PW! The following should work. Doing this in a hurry. There are other ways to do this...this is just one of them // grab and output a random image from the child page with ID 123 $image = $pages->get(123)->images->getRandom(); if($image) echo "<img src='$image->url'>"; See this page for more details on images... OK, am late in my reply Edit: Plus just seen you want images from any child pages; apologies. Will leave this response here though Edited May 13, 2013 by kongondo 2 Link to comment Share on other sites More sharing options...
diogo Posted May 13, 2013 Share Posted May 13, 2013 Welcome to the forum mcgarriers. You can do it like this: $parent = $pages->get(1001); // here I'm using the images array of the first child of 1001 to create a new empty array of the same kind (look on the cheatsheet for this method) $a = $parent->child->images->makeNew(); // iterate through all the pages and import their images to the created array foreach ($parent->children as $c) { $a->import($c->images); } // our array holds all the images that we want, Now you can get a random image $randomImage = $a->getRandom(); Edit: I've just seen Wanze's answer. The difference is that he is getting a random page, and then a random image. Like this, an image that is alone in a page has more chances of being shown than images in more crowded pages. While with my method they all have the same chances. 3 Link to comment Share on other sites More sharing options...
mcgarriers Posted May 13, 2013 Author Share Posted May 13, 2013 Many thanks for your replies, peeps Wanze, I get this error message: Error: Call to a member function getRandom() on a non-object (line 43 of *************\home.php) @diogo - many thanks for the reply. Using your code, I get the following error: Error: Call to a member function makeNew() on a non-object Link to comment Share on other sites More sharing options...
diogo Posted May 13, 2013 Share Posted May 13, 2013 This is because your field must be called something different from "images" 1 Link to comment Share on other sites More sharing options...
mcgarriers Posted May 13, 2013 Author Share Posted May 13, 2013 Ooooh. *slaps forehead* Let me fix that Link to comment Share on other sites More sharing options...
Wanze Posted May 13, 2013 Share Posted May 13, 2013 Are your sure that the field is named 'images'? Also if you have set 'Maximum files allowed' to 1, then the code needs little modification: // With max files set to '1' $img = $pages->get('parent=1010, images.count>0, sort=random')->images; // Check if we have an image if ($img->id) { echo "<img src='{$img->url}' alt='{$img->description}'>"; } else { echo "No page with image found..."; } 1 Link to comment Share on other sites More sharing options...
mcgarriers Posted May 13, 2013 Author Share Posted May 13, 2013 Perfect!!! Thank you very much for your help & patience throughout. I'm off to develop some more now Thanks, Wanze - I was a being a bit daft. The field name was 'image1' - all sorted now - thank you!!!! 2 Link to comment Share on other sites More sharing options...
diogo Posted May 13, 2013 Share Posted May 13, 2013 The forum is not showing when there are other answers while writing. How annoying... Link to comment Share on other sites More sharing options...
mcgarriers Posted May 13, 2013 Author Share Posted May 13, 2013 Me again Out of interest, how do I retrieve the Page ID associated with the Image? Link to comment Share on other sites More sharing options...
Soma Posted May 13, 2013 Share Posted May 13, 2013 The forum shows an ajax overlay when someone else posts while you're writing.. 1 Link to comment Share on other sites More sharing options...
diogo Posted May 13, 2013 Share Posted May 13, 2013 I know it does, but sometimes doesn't work, wish was the case now. Link to comment Share on other sites More sharing options...
mcgarriers Posted May 13, 2013 Author Share Posted May 13, 2013 Hi @diogo, Do you know if it's possible I can retrieve the Page ID associated with the Random Image that is selected? Link to comment Share on other sites More sharing options...
Soma Posted May 13, 2013 Share Posted May 13, 2013 Welcome to the forum mcgarriers. You can do it like this: $parent = $pages->get(1001); // here I'm using the images array of the first child of 1001 to create a new empty array of the same kind (look on the cheatsheet for this method) $a = $parent->child->images->makeNew(); // iterate through all the pages and import their images to the created array foreach ($parent->children as $c) { $a->import($c->images); } // our array holds all the images that we want, Now you can get a random image $randomImage = $a->getRandom(); Edit: I've just seen Wanze's answer. The difference is that he is getting a random page, and then a random image. Like this, an image that is alone in a page has more chances of being shown than images in more crowded pages. While with my method they all have the same chances. I thought it would be the opposite... However this solution is slower than Wanzes. Link to comment Share on other sites More sharing options...
diogo Posted May 13, 2013 Share Posted May 13, 2013 Hi @diogo, Do you know if it's possible I can retrieve the Page ID associated with the Random Image that is selected? like this: $randomImage->page (edit: gets the page itself, not the id, for the id use $randomImage->page->id) http://processwire.com/api/cheatsheet/#files However this solution is slower than Wanzes. Yep, it's really a mather of choosing what is most important in this case. If there will be an proximate number of images in each page, going with Wanze's method makes sense. If there will be pages with 1 image and others with 20, my method guarantees that the choice will be really random at the cost of being slower. 1 Link to comment Share on other sites More sharing options...
mcgarriers Posted May 13, 2013 Author Share Posted May 13, 2013 Fantastic! Out grabbing lunch right now but will try as soon as I'm back home. Thanks again sir Link to comment Share on other sites More sharing options...
mcgarriers Posted May 13, 2013 Author Share Posted May 13, 2013 Yay - $randomImage->page worked beautifully!! Thank you, Diogo Link to comment Share on other sites More sharing options...
Soma Posted May 13, 2013 Share Posted May 13, 2013 Here a little helper module that does it optimized using direct SQL. https://gist.github.com/somatonic/5568201 If you install module you can do: $helper = $modules->get("RandomImages"); // load module somewhere before // getRandomImages(count, field, parent) $image = $helper->getRandomImages(1,"images", 1001); echo "<img src='{$image->size(150,0)->url}'>"; Or to get more than 1 $helper = $modules->get("RandomImages"); // load module somewhere before // getRandomImages(count, field, parent) $images = $helper->getRandomImages(2,"images", 1001); foreach ($images as $key => $image) { echo "<img src='{$image->size(150,0)->url}'>"; } 5 Link to comment Share on other sites More sharing options...
diogo Posted May 13, 2013 Share Posted May 13, 2013 Maybe it would make sense to add a find() and get() method to $files. Think of how easy it would be to achieve a task like this: $randomImage = $files->find("page.parent=1010")->getRandom(); edit: there isn't any reference to files in the db. I guess it wouldn't be easy to do this... Link to comment Share on other sites More sharing options...
BFD Calendar Posted November 6, 2013 Share Posted November 6, 2013 Getting an error Error: Call to a member function getRandomImages() on a non-object on line $image = $helper->getRandomImages(1, "images", 1847); where '1847' is the parent of pages with an 'images' field. And the module doesn't show up in the list of modules although it's in the modules folder. Link to comment Share on other sites More sharing options...
BFD Calendar Posted November 10, 2013 Share Posted November 10, 2013 // grab and output a random image from the child page with ID 123 $image = $pages->get(123)->images->getRandom(); if($image) echo "<img src='$image->url'>"; This works excellent for something I'm doing. Just wondering if you can also grab the url of the page where the random image resides. To make the image clickable and go to the page, not the image itself. Link to comment Share on other sites More sharing options...
diogo Posted November 10, 2013 Share Posted November 10, 2013 Doesn't that code assume that you know already the page? Link to comment Share on other sites More sharing options...
BFD Calendar Posted November 10, 2013 Share Posted November 10, 2013 (edited) Doesn't that code assume that you know already the page? To you it probably does.... I'll give it some trial and error or perhaps a night sleep and see if it does to me as well. Thank you for the encouragement <a href='{$img->page->url}'><span class='newspapersubtitle'>{$img->description}</span></a> Edited November 10, 2013 by BFD Calendar Link to comment Share on other sites More sharing options...
opalepatrick Posted August 25, 2016 Share Posted August 25, 2016 On 13/05/2013 at 11:37 AM, Wanze said: $img = $pages->get('parent=1010, images.count>0, sort=random')->images->getRandom(); Hope it is OK to follow on from an old thread? This form of solution works great for me... but if I want also want to identify the image by tag how I would I do that? $sub_image = $pages->get("has_parent.id=$sub->id,template=product, sort=random")->product_images->getTag('prod')->getRandom(); does not work... bit flummoxed. Reason for wanting to do this is that there are different types of images in this field only some that are appropriate. 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