Jump to content


Photo

filtering rows from a repeater field


  • Please log in to reply
8 replies to this topic

#1 Rob

Rob

    Sr. Member

  • Members
  • PipPipPipPip
  • 122 posts
  • 34

  • LocationLondon, UK

Posted 18 July 2012 - 10:13 AM

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.

#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,186 posts
  • 1743

  • LocationSH, Switzerland

Posted 18 July 2012 - 03:06 PM

I think it's easy if you see the repeaters as pages array

Out of my head this can be done like this:
$elements = $page->repeater->find("checkboxfield=1");

@somartist | modules created | support me, flattr my work flattr.com


#3 slkwrm

slkwrm

    Sr. Member

  • Members
  • PipPipPipPip
  • 248 posts
  • 66

Posted 18 July 2012 - 06:45 PM

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


#4 Rob

Rob

    Sr. Member

  • Members
  • PipPipPipPip
  • 122 posts
  • 34

  • LocationLondon, UK

Posted 23 July 2012 - 06:05 AM

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!

#5 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,521 posts
  • 847

  • LocationVihti, Finland

Posted 03 December 2012 - 07:32 AM

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.

#6 DaveP

DaveP

    Sr. Member

  • Members
  • PipPipPipPip
  • 285 posts
  • 135

  • LocationChorley, UK

Posted 03 December 2012 - 08:29 AM

It couldn't be a dd/mm/yy versus mm/dd/yy problem, could it?
Twitter : Facebook : GitHub : G+ : Blog : Powered by C8H10N4O2 and C10H14N2

#7 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,521 posts
  • 847

  • LocationVihti, Finland

Posted 03 December 2012 - 08:43 AM

Nope, it compares timestamps.

#8 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3114

  • LocationAtlanta, GA

Posted 03 December 2012 - 09:38 AM

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/r...d8436789be2897c

#9 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,521 posts
  • 847

  • LocationVihti, Finland

Posted 03 December 2012 - 04:36 PM

Thanks Ryan for superfast reply & fix, I'll test this tomorrow.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users