CodeIgniter Forums
Returning value from a model to the controller after a few iteration of recursive function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Returning value from a model to the controller after a few iteration of recursive function (/showthread.php?tid=52798)



Returning value from a model to the controller after a few iteration of recursive function - El Forum - 06-27-2012

[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?


Returning value from a model to the controller after a few iteration of recursive function - El Forum - 06-27-2012

[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


Returning value from a model to the controller after a few iteration of recursive function - El Forum - 06-27-2012

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