Jump to content

Test if repeater has value and tick checkbox if found


celfred
 Share

Recommended Posts

Hi there,

I've been struggling for a couple of hours with something I guess one of you will find an answer rather quickly...

Let me explain : I'm creating a list of checkboxes according to the number of 'guests' pages belonging to the logged in user. In my page, I'd like some of the checkboxes checked by default IF the logged in user has already recorded them as checked (and this state is saved in a repeater field named 'guests', having 'guest_name' as text input.

My code is the following :

// If user has guests, list them for easy multiple selection
$guests = $pages->find("parent=/guests/, created_users_id=".$user->id);
$sel = 0;
if ( $guests->count() > 0 ) {
$sel = 1;
foreach ($guests as $guest) {
 foreach ($page->guests as $g) { // THIS IS A TEST TO SEE IF I CAN REACH THE REPEATER
 $out .= $g->guest_name; // IT WORKS AS EXPECTED
 }

 //$found = $page->matches("guests.guest_name=".$guest->title); // THIS DOESN'T WORK. WHY?
 if ( $found) {
 $checked = 'checked';
 } else {
 $checked = '';
 }
 $out .= '<label for="cb'.$guest->id.'"><input type="checkbox" name="friends[]" value="'. $guest->title .'" id="cb'.$guest->id.'" '. $checked .' />'.$guest->title.'</label>';
}
}

I've tried many different things : $page->is(), $pages->find(), tried to use has(), but I was incapable of making this work... I have a feeling the answer shouldn't be that hard, but as I said, I've been looking for it for a couple of hours now and I wish someone could give me a hand (again...) on that one. I hope I was clear enough in explaining the issue.

Thanks in advance.

Link to comment
Share on other sites

Well... What can I say except THANK YOU both for that. I sometimes feel ashamed of not finding the answers by myself. Honestly, I spend a lot of time searching for them... Don't think I come and ask for help as a 'shortcut' ;-)

Anyway, I wasn't that good on that one, but I've just learned so much thanks to both of you.

Thanks!

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