nabo Posted April 14, 2017 Share Posted April 14, 2017 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 More sharing options...
Zeka Posted April 14, 2017 Share Posted April 14, 2017 HI @nabo It looks like you can use subfield selectors: https://processwire.com/api/selectors/#subfield $pages->find("template=store, repeater_fied.hours=10"); 3 Link to comment Share on other sites More sharing options...
nabo Posted April 15, 2017 Author Share Posted April 15, 2017 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 More sharing options...
Zeka Posted April 15, 2017 Share Posted April 15, 2017 HI @nabo // $page - is page with store template $dayRepeaters = $page->repeater_field->find("days=friday"); foreach($dayRepeaters as $rp) { echo $rp->hours; } Does it suit your need? 1 Link to comment Share on other sites More sharing options...
nabo Posted April 15, 2017 Author Share Posted April 15, 2017 Hi @Zeka very easy indeed!!! Thank you very much! Link to comment Share on other sites More sharing options...
PWaddict Posted August 9, 2017 Share Posted August 9, 2017 @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 More sharing options...
Zeka Posted August 9, 2017 Share Posted August 9, 2017 Hi @PWaddict Are you sure that you have repeater items that should match your selector? Your code looks good to me. Link to comment Share on other sites More sharing options...
PWaddict Posted August 9, 2017 Share Posted August 9, 2017 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 More sharing options...
Robin S Posted August 9, 2017 Share Posted August 9, 2017 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. 8 1 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