froot Posted November 19, 2022 Share Posted November 19, 2022 can't get my code to effectively use ->parents() API I have this function function getSingleValue($item) { $foo = findValue($item, "foo"); return $foo; } and then this one, but it doesn't work with the selector function findValue($item, $field) { if (isset($item->$field) && $item->$field != '') { return $item->$field; } else { $parent = $item->parents("$field!=", true); if ($parent != null) { return $parent->$field; } } return null; } echo getSingleValue('bar'); // my expected value however, with the loop, it works: function findValue($item, $field) { if (isset($item->$field) && $item->$field != '') { return $item->$field; } else { $parents = $item->parents(true); foreach($parents as $parent) { if (isset($parent->$field) && $parent->$field != '') { return $parent->$field; } } } return null; } echo getSingleValue('bar'); // my expected value Any ideas why? Link to comment Share on other sites More sharing options...
Jan Romero Posted November 19, 2022 Share Posted November 19, 2022 1 hour ago, fruid said: $parent = $item->parents("$field!=", true); remove the s. https://processwire.com/api/ref/page/parents/ https://processwire.com/api/ref/page/parent/ 2 Link to comment Share on other sites More sharing options...
froot Posted November 20, 2022 Author Share Posted November 20, 2022 right, don't know how that happened… thanks! However, it's not just that. I have a float field on the template and on the parent and grandparent template. I set the field's settings to differentiate between empty and 0. So 0 and '' are NOT the same. I leave the field empty on the template. then when I do… function findValue($item, $field) { if ($item->$field >= 0) { // should not meet condition because field is empty and not 0 nor bigger return $item->$field; } else { $parent = $item->parent("$field!=", true); if ($parent != null) { return $parent->$field; } } return null; } it always goes to the if-code-block, how can that be? Link to comment Share on other sites More sharing options...
froot Posted November 20, 2022 Author Share Posted November 20, 2022 And then also, there's one scenario where the value IS indeed 0, and NOT empty. So I need the condition to be true if the field value is literally 0 and not true if the field is actually not populated. if ($item->$field >= 0) { … } // this is true even if the field is not populated – not expected if ($item->$field >= 0 && $item->$field != '') { … } // this is false even when the field value is actually 0 – not expected if ($item->$field != '') { … } // this is false even when the field value is actually 0 – not expected It seems like which ever way I twist and turn it, I can't get it right. (I realise this is now off topic because I'm not even using the ->parent() API with selector anymore, because it seems to make this matter even harder) Link to comment Share on other sites More sharing options...
froot Posted November 21, 2022 Author Share Posted November 21, 2022 this is simply not true, for floats at least, it does NOT make a difference between 0 and empty value ? Link to comment Share on other sites More sharing options...
Jan Romero Posted November 21, 2022 Share Posted November 21, 2022 Works for me... However, a blank value will give you an empty string, so you need to take that into account when you write your comparisons. In PHP an empty string IS equal to 0 with loose comparison, so '' >= 0 is TRUE. To test for blank values, you can just check $page->myfloat === '' or is_string($page->myfloat) or probably a million other things, considering we’re in PHP land. I agree that this isn’t strictly mentioned your screenshot, but it should be. edit: lol turns out they changed this with PHP 8.0, so watch out for that too 1 Link to comment Share on other sites More sharing options...
froot Posted November 22, 2022 Author Share Posted November 22, 2022 if ($item->$field >= 0 && $item->$field !== '') { this did the trick, thanks a lot for the input @Jan Romero 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