thetuningspoon Posted February 16, 2013 Share Posted February 16, 2013 Hi guys, I asked a similar question to this a little while back, but this time it's a bit different (and hopefully better worded). I have a page with a repeater on it containing 2 fields. Using the API, I want to find the value of field 1 where field 2 contains a certain specified string. What is the best way of doing this? I suppose this is the same as asking how to find the value of one field on a page given the value of another field on that page, since PW handles repeating fields as pages under the hood. Any help is much appreciated. Link to comment Share on other sites More sharing options...
Joss Posted February 16, 2013 Share Posted February 16, 2013 Okay, far too late at night for this! But, since it is returned as an array, I suppose you could use array_search to find the key and then use the key to return the field data. Umm something like that! try here: http://php.net/manual/en/function.array-search.php (Soma is floating around - he will know) Link to comment Share on other sites More sharing options...
Soma Posted February 16, 2013 Share Posted February 16, 2013 I haven't done so much with repeater but this gave me the value I needed. // find pages with a certain value in one of the fields of a repeater item. $found = $pages->find("myrepeater.field2=foo"); echo "found: ".count($found); foreach($found as $f){ // find the repeater item field2 with value foo $r = $f->myrepeater->find("field2=foo")->first(); echo $r->field1; } Depends what field values and type you have, but this does work. 2 Link to comment Share on other sites More sharing options...
Soma Posted February 16, 2013 Share Posted February 16, 2013 if you only have to find THE ONE value on one page you could do this. And this is lovely about PW echo $pages->get("myrepeater.field2=bar")->myrepeater->get("field2=bar")->field1; gn8 3 Link to comment Share on other sites More sharing options...
Joss Posted February 16, 2013 Share Posted February 16, 2013 ooh, sweet! Link to comment Share on other sites More sharing options...
diogo Posted February 16, 2013 Share Posted February 16, 2013 echo $pages->get("myrepeater.field2=bar")->myrepeater->get("field2=bar")->field1; we should make a compilation of soma's one-liners to rival with this https://www.youtube.com/watch?v=GeeyWvo1rNg 4 Link to comment Share on other sites More sharing options...
Joss Posted February 16, 2013 Share Posted February 16, 2013 echo $pages->get("myrepeater.field2=bar")->myrepeater->get("field2=bar")->field1; we should make a compilation of soma's one-liners to rival with this https://www.youtube.com/watch?v=GeeyWvo1rNg Yep, Soma, the coders equivalent of Steven Wright. 1 Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 16, 2013 Author Share Posted February 16, 2013 Brilliant Soma! Thanks. As a follow up question (this is the last piece of the puzzle), can I use a variable in a selector? If so, how? EDIT: Nevermind, I see that variables in selectors work just fine. It was the particular variable I was feeding it that was causing the problem Link to comment Share on other sites More sharing options...
ryan Posted February 17, 2013 Share Posted February 17, 2013 Nevermind, I see that variables in selectors work just fine. So long as that variable is in a double quote string (or concatenated to the string). PHP doesn't parse variables in single quote strings. If the variable you are putting in might have characters that could conflict with selector strings (like commas, or |) surround the value in single quotes. like something='$something'. If the variable is coming from user input, be sure to sanitize it. For a string, you'd want to use $something = $sanitizer->selectorValue($input->post->something); Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 18, 2013 Author Share Posted February 18, 2013 So long as that variable is in a double quote string (or concatenated to the string). PHP doesn't parse variables in single quote strings. Yes, I should have included this caveat. Thanks for extra info, Ryan. Edit: Actually, although I had this working, I was still puzzled by Soma's solution. I realized after some trial and error that it is more complex than I needed. Since I already know the page that the repeater is on, I was able to simplify the code to this: $pages->get("name=mypage")->myrepeater->get("field2=bar")->field1; Which makes it a little clearer and is hopefully more efficient for my needs. I am also using the *= operator ("field2*=bar") to search within the second field, since it is actually a textarea containing a list of items. Link to comment Share on other sites More sharing options...
ryan Posted February 18, 2013 Share Posted February 18, 2013 $pages->get("name=mypage"); isn't a very reliable thing to do just because that query is within the scope of your entire site. Page names are only guaranteed to be unique within the same parent. So you'd be much better off doing one of these instead, which would be guaranteed unique: $pages->get("/path/to/mypage/") ... // retrieve by page path $pages->get(123) ... // retrieve by page ID Since the 'name' is guaranteed to be unique within the same parent, it would be perfectly fine to do this though, where $page is some page with children: $page->child("name=mypage") Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 18, 2013 Author Share Posted February 18, 2013 I see what you're saying, although the name I'm using is pretty unique, so I'm not overly concerned. I like that it's easier than an ID to see what's happening in the code and doesn't have the drawback that paths have in case I want to move the page to somewhere else in the tree. Is there a performance benefit to using ID or path instead? Link to comment Share on other sites More sharing options...
ryan Posted February 20, 2013 Share Posted February 20, 2013 I see what you're saying, although the name I'm using is pretty unique, so I'm not overly concerned. I like that it's easier than an ID to see what's happening in the code and doesn't have the drawback that paths have in case I want to move the page to somewhere else in the tree. Is there a performance benefit to using ID or path instead? It shouldn't matter in terms of performance. If your "name" is one you aren't going to be using again, then you should be totally fine to use it. 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