Jump to content

Finding the value of one field on a page where another field contains a specified string


thetuningspoon
 Share

Recommended Posts

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

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. 

  • Like 2
Link to comment
Share on other sites

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

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

$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

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

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

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