Jump to content

can't get my code to effectively use ->parents() API


fruid
 Share

Recommended Posts

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

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

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

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

  • Like 1
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...