Pete Posted December 4, 2011 Share Posted December 4, 2011 Hi folks I'm trying to grab 3 random but unique images from the images field on a website's homepage. Here's my code so far which does a bit of iteration, thumbnailing and cropping: $homepageimgs = $pages->get("/")->images; /*foreach ($homepageimgs as $k => $v) { echo $k; }*/ $imgarray = array(); for ($i = 0; $i < 3; $i++) { $imgarray[$i] = $homepageimgs->getRandom(); $scale = 262/$imgarray[$i]->width(); $imgarray[$i] = $imgarray[$i]->size(262,round($scale * $imgarray[$i]->height())); $imgarray[$i] = $imgarray[$i]->size(262, 149); unset($homepageimgs[$imgarray[$i]->name]); } The issue is that I want 3 unique images, so I need to remove whichever random image is selected during each iteration from the $homepageimgs array so it can't be randomly selected again. Sadly that last line of code returns an error. Any ideas? I was pretty sure the filename is also the key (as evidenced during the 3 lines of commented-out code). I also tried unset($homepageimgs->$imgarray[$i]->name); which is probably ridiculous, however my OO PHP knowledge is severely lacking hence why I tried it anyway Link to comment Share on other sites More sharing options...
Pete Posted December 4, 2011 Author Share Posted December 4, 2011 Never mind - I forgot PW allows you to do this instead to get $n random images. $a->getRandom($n) Code is now as follows and works perfectly: $homepageimgs = $pages->get("/")->images; $imgarray = $homepageimgs->getRandom(3); $resizedimgarray = array(); $i = 0; foreach ($imgarray as $img) { $scale = 262/$img->width(); $resizedimgarray[$i] = $img->size(262,round($scale * $img->height())); $resizedimgarray[$i] = $img->size(262, 149); $i++; } Link to comment Share on other sites More sharing options...
Soma Posted December 4, 2011 Share Posted December 4, 2011 I'm asking myself why youre trying to make a loop to get 3 random images. Can't you just use $result = $page->images->getRandom(3) ? Then loop the returned three images? Haven't you used the Cheatsheet??? Link to comment Share on other sites More sharing options...
Pete Posted December 4, 2011 Author Share Posted December 4, 2011 Hehe, I was replying as you were replying. The cheatsheet did indeed hold the answer but for some reason I was originally trying to search for "images" on the cheatsheet before I remembered it would be an array anyway (oops). I blame the horrible cold I've currently got impacting on my brain's ability to think straight 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