Jump to content

Find repeater


nabo
 Share

Recommended Posts

Hi

I've got a template for a store and I use repeater for opening hours.
When I'm on the store page I would like to search inside this repeater to find the hours related to a precise day.

Is it possible with selectors?

Link to comment
Share on other sites

Hi @Zeka

thanks, but in this case I think I will find all pages that with template store and repeater_field.hours = 10.
I come with an example. These are the fields in my repeater:

days
hours

I'm in the store page and I want to find only hours related to the day, for example friday.
The example you provide me is not what I need :(

Link to comment
Share on other sites

  • 3 months later...

@Zeka I followed your example but it doesn't work. I want to get all the upcoming dates from the current's page repeater:

<?php if(count($page->shows_dates_repeater)) {
  $dates = $page->shows_dates_repeater->find("shows_date>=today, sort=shows_date");
  foreach($dates as $date) {
?>

<?= $date->getFormatted("shows_date") ?>

<?php } 
} ?>

I get nothing with the above code, not even an error. If I change the shows_date field operator to < then I get all the dates (past & upcoming).

Link to comment
Share on other sites

55 minutes ago, Zeka said:

Hi @PWaddict

Are you sure that you have repeater items that should match your selector?

Your code looks good to me.  

Yes the repeater items (shows_date) have the following dates:

3 Aug 2017
9 Aug 2017
4 Sep 2017
15 Sep 2017

So with this selector: shows_date>=today it should display all the dates except the first one (3 Aug 2017) but nothing gets displayed. It's like that the today selector isn't working on repeaters :(

EDIT: I replaced the "today" selector with today's hardcoded unix timestamp and it worked :)

So here is the solution:

$today = strtotime("today");
$dates = $page->shows_dates_repeater->find("shows_date>=$today, sort=shows_date");

 

Link to comment
Share on other sites

2 hours ago, PWaddict said:

It's like that the today selector isn't working on repeaters

Just some explanation because it is interesting... :)

This isn't something that is specific to Repeaters - it applies to any selector used on a PageArray/WireArray (a Repeater field returns a PageArray).

When you do $some_pagearray->find() this is different to a $pages->find() - the method name is the same but they are actually totally different methods. See find() method in the PageFinder class vs the WireArray class.

When using $pages->find() the method refers back to the fieldtype for each queried field and the fieldtypes can do some special preparation to the queried value. In the case of a Datetime field the value is passed through strtotime() if it is a string. So this allows "today" to be converted to a timestamp.

But when using $some_pagearray->find() this process does not happen, so the queried value must be a timestamp.

Another gotcha to watch out for is using the page status as a string in a selector. With $pages->find() you can do something like "status!=hidden" but this won't work with a selector in $some_pagearray->find(). 

I think it would be nice if things like this did work with PageArrays - there is an open request for it.

  • Like 8
  • Thanks 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...