Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to get the main parent of chosen subcategory. I made function which recursively loop the databasse and it even get the ID but I can only echo it. I cannot return it into variable. I need to work further with the returned ID. Here is my code.

public function check_parent($parent)
{
    $q = $this->db->get_where('ci_categories', array('cat_id'=>$parent));
    $r = $q->row();
    if ($r->cat_child > 0)
    {
        $this->check_parent($r->cat_child);
    }   else {
        echo $parent;
    }
}

When I use return $parent in else I get null. Any ideas ?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

if you want to return the value you should return it on both places

public function check_parent($parent)
{
$q = $this->db->get_where('ci_categories', array('cat_id'=>$parent));
$r = $q->row();
if ($r->cat_child > 0)
{
    return $this->check_parent($r->cat_child);
}   else {
    return $parent;
}
share|improve this answer
 
Such a simply answer. Thanks –  DeiForm Jul 11 at 10:35
 
@user217536 don't forget to accept the answer :) –  DevZer0 Jul 11 at 10:37
1  
Dont worry I would do it right now but there is 10minutes limit –  DeiForm Jul 11 at 10:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.