• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.