benbyf Posted June 1, 2014 Share Posted June 1, 2014 Hi, I'm trying to combine two find() arrays in a random order but currently can only make it give me back the same order each time. here's what im working with: $works = $pages->find('template=work'); $articles = $pages->find('template=article'); $works->prepend($articles); $randWorks = $works->getRandom(count($works)); foreach($randWorks as $child) { $class = $child === $page->rootParent ? " class='active'" : ''; echo "<a$class href='{$child->url}'>{$child->title}</a>"; } Link to comment Share on other sites More sharing options...
adrian Posted June 1, 2014 Share Posted June 1, 2014 Are you looking for findRandom rather than getRandom? http://cheatsheet.processwire.com/?filter=findrandom Link to comment Share on other sites More sharing options...
benbyf Posted June 1, 2014 Author Share Posted June 1, 2014 like this? $randWorks = $works->findRandom(count($works)); doesn't seem to work, still in same (non random) order Link to comment Share on other sites More sharing options...
adrian Posted June 1, 2014 Share Posted June 1, 2014 Sorry, forgot to mention that you also need to shuffle if you want random order rather than just random selection. So do this once you have $randWords populated: $randWorks->shuffle(); 2 Link to comment Share on other sites More sharing options...
OrganizedFellow Posted October 14, 2014 Share Posted October 14, 2014 My comment is related to this topic. I can not for the life of me figure out how to randomize something. $onetestimonial = $pages->find("template=testimonial,limit=1"); foreach ($onetestimonial as $testes) { echo $testes->title; } I can get that far. But I can not figure out how to randomize the output. There are currently 10 Testimonials. I'd like to display only one randomly. I've seen $a->shuffle()and getRandom() but haven't the slightest idea how to use them. Every attempt has resulted in some page error. UGH, please help a PHPNEWB? lol Link to comment Share on other sites More sharing options...
OrganizedFellow Posted October 14, 2014 Share Posted October 14, 2014 GOSH DARNIT!!!!!!!!! I solved it. I previously spent about an hour reading over the API and cheatsheet. Then I came upon sort=random I hate it when this happens: $onetestimonial = $pages->find("template=testimonial,limit=1,sort=random"); 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