0

so, this is weird, I have this method called treetrunk that runs up a parent child relationship and is supposed to return an array if ids as a "branchPath" - the method seems to be working just fine as a var_dump in my terminating condition shows the proper array. However if i try to call the method the returned array is "NULL" - I really dont get it..

Model method:

function treeTrunk($id){//1st id in is page id
    if($id == '0'){
        var_dump($this->branchPath); //this shows perfect array 
        return $this->branchPath; //this returns null
    }
    else{
        $q = $this->getWhere(array('id'=>$id),null,null);
        array_push($this->branchPath, $q[0]['pageParent']);
        $this->treeTrunk($q[0]['pageParent']);
    }       
}

Calling via controller:

$d = $this->pages_mdl->treeTrunk('150');
var_dump($d); // <- this == NULL

var_dump from within method outputs "array(3) { [0]=> string(3) "148" [1]=> string(3) "146" [2]=> string(1) "0" } "

1
  • Doesn't this just return null because you're sending in '150' while the if statement is looking for '0'? Thus nothing is ever returned. What does the else portion do, call itself recursively? Does that ever get anything returned? Commented Aug 20, 2013 at 5:19

2 Answers 2

1

You are not returning anything in else part.

else{
    $q = $this->getWhere(array('id'=>$id),null,null);
    array_push($this->branchPath, $q[0]['pageParent']);
    $this->treeTrunk($q[0]['pageParent']);
} 

should be

else{
    $q = $this->getWhere(array('id'=>$id),null,null);
    array_push($this->branchPath, $q[0]['pageParent']);
    return $this->treeTrunk($q[0]['pageParent']);
} 

As posted in you question you are passing 150 to treeTrunk() function, so it goes to else part and gives you null result. The if part will evaluate when you pass 0 to treeTrunk() function.

Sign up to request clarification or add additional context in comments.

1 Comment

"getWhere" gets the parent id of the id - it gets '0' when it reaches a top level page. Returning the recursive call works though. I'll take that.
0

That is supposed to be NULL

else{
    $q = $this->getWhere(array('id'=>$id),null,null);
    array_push($this->branchPath, $q[0]['pageParent']);
    $this->treeTrunk($q[0]['pageParent']);  // You have to return this value as well
}   

When you don't return that array, what else do you expect it to send back ? add a return statement there. When you are dealing with recursion then your last call returned the value to your second last call, but did that call return it back to the original caller? so add a return there

   return $this->treeTrunk($q[0]['pageParent']);

And also remove the quotation marks around integer values, not only does it look bad, it sometimes stops working as expected in PHP then.

Demo

1 Comment

Brilliant! ~ though I still can't wrap my head around why the var dump gave me what I was looking for but not the return... i also did not know you could return another call to the method - +1 for teaching me something new!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.