Welcome Guest, Not a member yet? Register   Sign In
Returning value from a model to the controller after a few iteration of recursive function
#1

[eluser]Unknown[/eluser]
I have a function that search category_id of top level parent category. But after 1 iteration it doesn`t return value to controller.

Controller:
Code:
$top_parent = $this->catalog_model->top_parent($catalog_id);

Model:
Code:
function top_parent($category_id)
    {
        $data = $this->db->select('category_id, title, parent')
                         ->where('category_id',$category_id)
                         ->get('categories')
                         ->row_array();              

        if($data['parent'] == 0){
            return $data['category_id'];  
        }
        else{
            $this->top_parent($data['parent']);
        }

    }

How can I return found value to controller with recursive function?
#2

[eluser]vitoco[/eluser]
Just return the value of the recursive call
Code:
function top_parent($category_id)
    {
        $data = $this->db->select('category_id, title, parent')
                         ->where('category_id',$category_id)
                         ->get('categories')
                         ->row_array();              

        if($data['parent'] == 0){
            return $data['category_id'];  
        }
        else{
            return $this->top_parent($data['parent']);
        }

    }

Saludos
#3

[eluser]Unknown[/eluser]
You are a genius! And I need sleep better... Anyway, thank You!




Theme © iAndrew 2016 - Forum software by © MyBB