Jump to content

filtering rows from a repeater field


Rob
 Share

Recommended Posts

I have a repeater field where one of the constituent fields is a checkbox for whether the repeater item is "live" or not.

What I want to do is fetch only those items from the repeater field where the "live" value is checked. Is this possible?

Thanks.

Link to comment
Share on other sites

Yeah, Soma's right!

You can do it this way:

foreach ($page->myrepeater as $item) {
if ($item->visible) {
	echo "<p>$item->title</p>";
    //or do something else...
}
}

or this:

foreach ($page->myrepeater->find('visible=1') as $item) {
 echo "<p>$item->title</p>";
   //or do something else...
}
  • Like 1
Link to comment
Share on other sites

Thanks chaps!

I had tried that and thought it wasn't working, but in fact it was. What was happening was that I had pulled an entire page of data, then was trying to "overwrite" one part of it with the filtered version of itself. Trying to overwrite an object with an array = bad news!

Link to comment
Share on other sites

  • 4 months later...

I can't seem to get repeater selectors work correctly with date fields. I have a repeater with four fields:

  • photo (single image)
  • bannerlink (page field)
  • startdate (date)
  • enddate (date)

What I want to achieve is getting one random repeater which has startdate < time() < enddate. So it should have startdate which is before this very moment and enddate that is after this moment.

Results I am getting are strange:

// Get the current timestamp
$time = time();

// I put my repeater pages into variable
$banners = $page->shop_banners;

// This is where things start to go strange
$less_banners = $banners->find("enddate>0, enddate>$time");

foreach($less_banners as $p) {
echo "START: " . $p->startdate . " < NOW: " . date("j.m.Y H:s", $time) . " < END: " . $p->enddate . "<br>";
}

/*
I have total 6 items, where 4 doesn't have anything on enddate, One has enddate before this moment and another has enddate after this moment.

With that loop I get rows like:
START: 7.8.2012 02:51 < NOW: 3.12.2012 14:42 < END: 4.12.2012 17:48
START: 2.12.2012 00:00 < NOW: 3.12.2012 14:42 < END: 2.12.2012 23:00 // THIS LINE SHOULD NOT BE HERE?
*/

// Then if I put it other way around:

$less_banners = $banners->find("enddate>0, enddate<$time");

foreach($less_banners as $p) {
echo "START: " . $p->startdate . " < NOW: " . date("j.m.Y H:s", $time) . " < END: " . $p->enddate . "<br>";
}

/* I get zero results */

Show me where I am doing it wrong (I still have a bad gut feeling about doing something very silly here). I have tested this on pretty recent stable version and also latest dev version. I do similar stuff in many places (with events-item template etc), so I do know it should work - but for some reason I don't can get it working with repeaters.

  • Like 1
Link to comment
Share on other sites

Antti, it looks like this is a result of output formatting being on, so it's doing a string comparison rather than a time comparison. This is not intended and not a detail people should not have to account for, so I'd consider it a bug. Thanks for tracking it down. The bug is only for in-memory finds on date fields, not database finds, as it's working how it should there already. I've put a fix in the latest commit to the dev branch of you want to try it out. It shows two files updated, but the only one that matters here is /wire/core/Array.php

https://github.com/ryancramerdesign/ProcessWire/commit/393e420fc8ab60670dc4c874dd8436789be2897c

  • 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...