Jump to content

Inserting a random $item into pageArray at a random point


a-ok
 Share

Recommended Posts

Hi folks hope all is well.

I'm wanting to insert a random repeater row into a page selector loop ($pages->find()) at a random point (after the 1st, after the 5th, after the 2nd – different each reload).

I have managed to get a random repeater row and now I need to insert it into my returned pages...

$ctas = $pages->get("name=site-settings")->settings_ctas;
$cta = $ctas->getRandom();
$events = $pages->find("template=events-detail, events_detail_dates.events_detail_dates_start_date>=today, sort=events_detail_dates.events_detail_dates_start_date, sort=name"); // Return in-date events only

I thought about using $a->insertAfter($item, $existingItem) and setting $existingItem to a random integer? Is this is the best way do you reckon?

http://cheatsheet.processwire.com/pagearray-wirearray/setting-and-modifying-items/a-insertafter-item-existingitem/

Link to comment
Share on other sites

You can add the random repeater to the $events array and shuffle it after that.

$events->append($cta);
$events->shuffle();

Sorry, I just saw that you are ordering the events. So this will not work. In this case I think using insertAfter() is a good solution.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...