Jump to content

help - fetching fields randomly from a repeater


John W.
 Share

Recommended Posts

Following the example from the PW docs I set up a a repeater.

 I'm trying to fetch random fields from a repeater, for instance, display 4 random customer testimonials.

Here is the basic setup  in PW:

created these fields:

  • testimonial_text
  • testimonial_name

I then added them to a repeater field called:

  • testimonials_repeater

Next I added testimonials_repeater to a template called 

  • testimonials


Last, I created a new page called Testimonials using the testimonials template.

As far as fetching all the testimonials I'm doing this:

 

<?php
  	$testimonials = $pages->find("template=testimonials");
        foreach($testimonials as $trepeater) {
          foreach($trepeater->testimonials_repeater as $t) {
			echo $t->testimonial_text .  ' -' . $t->testimonial_name;
		}}
?>

 

Could some please give me some guidance on how to pull, for instance, 4 random testimonials?

I mean, I could stick the results in an array[] inside the foreach loops, then random sort the array, then output only 4, but there has to be a better way using PW.

 

Also, is there a way to limit the number of records (customer testimonials) that can be added by the user? 

 

Repeaters just always seem to trip me up. :/

 

thanks much!

 

 

Link to comment
Share on other sites

16 minutes ago, Zeka said:

Hi

If I correctly understood your setup:


$testimonials = $page->testimonials_repeater->getRandom(4);

 

No. The only page is called testimonials, which uses a template named testimonials which contains the field testimonials_repeater.

 

Thus something like:

$testimonials = $page->testimonials->testimonials_repeater->getRandom(4)

also tried

$testimonials = $page->testimonials->getRandom(4); 

but, both return an undefined function for getRandom();

 

Link to comment
Share on other sites

2 minutes ago, Zeka said:

Hi @holmescreek

Do you use this code on pages with "testimonials" template? 

 

Thanks, that is exactly what I was needing, Zeka.  I kept trying to use $pages->find and set a limit=4, sort=random type of selection. Your solution above worked.

Link to comment
Share on other sites

40 minutes ago, adrian said:

If you have lots of repeater items, this will be more efficient:


$pages->get("/testimonials/)->testimonials_repeater->find("limit=4, sort=random");

 

Will it though? That's true for $pages>find() selectors but I think for a repeater field the whole PageArray is loaded to memory regardless. Or is there another efficiency?

  • Like 1
Link to comment
Share on other sites

  • 3 months later...
On 13/04/2017 at 10:53 PM, adrian said:

If you have lots of repeater items, this will be more efficient:


$pages->get("/testimonials/)->testimonials_repeater->find("limit=4, sort=random");

 

Exactly what I was looking for, thanks :)

  • 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

×
×
  • Create New...