0

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 ?

1 Answer 1

0

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;
}
2
  • Such a simply answer. Thanks Commented Jul 11, 2013 at 10:35
  • 1
    Dont worry I would do it right now but there is 10minutes limit Commented Jul 11, 2013 at 10:39

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.