celfred Posted November 23, 2012 Share Posted November 23, 2012 Hello, I am struggling again with my lack of understanding of PHP developement (I guess)... I would just like to get rid of a field (part of a repeater) in my page if I find a particular value. My code is the following : $name = $pages->get($id)->title; // THIS IS THE VALUE I SEARCH FOR $found = $pages->find('guests.guest_name='.$name); // THESE ARE THE PAGES CONTAINING THE VALUE foreach ($found as $f) { // LOOP THROUGH THE PAGES foreach ($f->guests as $g) { // LOOP THROUGH THE REPEATER TO GET RID ONLY OF THE FIELD CONTAINING THE NAME I AM LOOKING FOR if ($g->guest_name == $name) { // IF I FIND THE NAME IN THE FIELD $f->remove(guests.guest_name); // GET RID OF THE FIELD. THIS IS THE LINE I REALLY DON'T UNDERSTAND : REMOVE? DELETE? HOW TO USE IT...? } } } I've tried to show my way of seeing things here, since I'm not sure I'm thinking well about my issue. Feel free to put me on the right tracks ;-) And as always, thanks in advance for your help. Link to comment Share on other sites More sharing options...
celfred Posted November 24, 2012 Author Share Posted November 24, 2012 Well... I guess I found something by myself : This seems to work as expected : $name = $pages->get($id)->title; $found = $pages->find('created_users_id='.$user->id.',guests.guest_name='.$name); foreach ($found as $f) { foreach ($f->guests as $g) { if ($g->guest_name == $name) { $g->delete('guest_name'); } } } I re-read (re-re-re-re-read, actually ;-)) the documentations and from what I understood, 'remove' was not to be used for fields. I used 'delete' instead and it looks ok. Regarding my nested 'foreach', although I feel like there would be a really better way to do this, it seems to do the work... Link to comment Share on other sites More sharing options...
Soma Posted November 24, 2012 Share Posted November 24, 2012 I'm not sure about your term "delete field"? Are you sure you want to delete the "field" and not just empty the value or remove the entry from a repeater list? Because if you are talking about a repeaters fields and you delete one of the fields it has, it will be gone for all entries. If I understand correctly you only want to "remove" an entry in the list (repeater)? Reading here http://processwire.c...ypes/repeaters/ there's no "delete", but there's a remove method to remove and certain entry in a repeater. Also repeaters are "pages" and behave like a pagearray and you can use find(selector) and get(selector) to find entries. So the code would be like this: $name = $pages->get($id)->title; $found = $pages->find('created_users_id='.$user->id.',guests.guest_name='.$name); foreach ($found as $f) { foreach ($f->guests as $g) { if ($g->guest_name == $name) { $f->guests->remove($g); // remove the entry from the repeater $f->save(); // without saving page, nothing will happen } } } Or this maybe $name = $pages->get($id)->title; $found = $pages->find('created_users_id='.$user->id.',guests.guest_name='.$name); foreach ($found as $f) { $entries = $f->guests->find("guest_name=$name"); // do a find on repeater, like with pages foreach ($entries as $entry) { $f->guests->remove($entry); // remove the entry from the repeater $f->save(); // without saving page, nothing will happen } } Or if you are sure there's only 1 page and 1 entry at any time to be found anyway $name = $pages->get($id)->title; $found = $pages->get("created_users_id=$user->id,guests.guest_name=$name"); // get the 1 page if($found) $entry = $found->guests->get("guest_name=$name"); if($entry) { $found->guests->remove($entry); $found->save(); } All written in browser, so not tested. 3 Link to comment Share on other sites More sharing options...
celfred Posted November 24, 2012 Author Share Posted November 24, 2012 Thanks a lot for this nice answer! Because if you are talking about a repeaters fields and you delete one of the fields it has, it will be gone for all entries. I don't really understand this... I think maybe my problem comes from the fact that my repeater only has 1 text filed (guest_name). Actually, my pages record the names of 'guests' and when a user deletes a guest, I'm trying to 'delete' (or remove?) the same name in every page belonging to the logged in user where the name is found... Since I had multiple names in each page, I decided on using a repeater. Maybe that was not a good idea ? If I understand correctly you only want to "remove" an entry in the list (repeater)? Exactly! But I still don't undertand the difference with delete (for the moment). I've tried what you suggest : - Code 01 & Code 02 work fine! And I understand how they work. Thanks for reminding me that the repeater acts as a 'Page' and a pagearray. I think that's the part I wasn't aware of during my tests and that your answer helped me 'tidying up' what I've read so far. - Code 03 : I haven't really tried since I have more than 1 pages containing the 'guest_name' value. During my tests, I understood that 'get' limited the search to 1 page whereas 'find' returned a pagearray. Anyway, thanks again for the answer. I'm happy the code works. Still, I'd like to understand better why my previous code (with the 'delete' version, and no 'save()') ended up working (from what I've tested so far... Maybe there would be other issues raised in a different context) and how different it is from your 'remove and save' solutions. 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