CodeIgniter Forums
Unable to return array from function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Unable to return array from function (/thread-18837.html)



Unable to return array from function - El Forum - 05-20-2009

[eluser]Unknown[/eluser]
Hi,

I am new to codeigniter and a beginner in php so would appreciate your help with this basic problem:

I am unable to return an array from a function in a model to the controller. The code for calling the function is
Code:
$getdata=array();
$data['parent']=$this->MCats->getparent(12,$getdata);
The code the model uses to return the data is simply:
Code:
return $breadcrumb;
If I use print_r to display the $breadcumb array immediately prior to it being returned I get the result:
Quote:Array ( [0] => test [1] => games| [2] => fun| )

I'd be grateful for any advice to solve this problem.


Unable to return array from function - El Forum - 05-20-2009

[eluser]Dam1an[/eluser]
Your're going to need to shouw a bit more code so we can see whats really going on... The whole breadcrumb function would be nice Smile


Unable to return array from function - El Forum - 05-20-2009

[eluser]Unknown[/eluser]
Hi Dam1an, many thanks for replying. Here's the full function:
Code:
function getparent ($id, $breadcrumb=array()) {
$this->db->where('id', $id);
$Q = $this->db->get('categories');
$row =     $Q->row_array();
// Add the category name to string
// Don’t add a | to the first category.
if (count($breadcrumb) == 0) {
    $breadcrumb = array();
    $breadcrumb[count($breadcrumb)]=$row['name'];
}
else {
    $breadcrumb[count($breadcrumb)]=$row['name']. "|";
}
// Figure out if our category has a parent category or not.
$this->db->select('parentid','name');
$this->db->where('id', $id);
$Q = $this->db->get('categories');
$row = $Q->row_array();
// If this value is 0, we are at the top of the tree…otherwise call the function again.
$parentCat = $row['parentid'];      
if ($parentCat == 0)      {
      // This is our base case, there is no parent category.
    print_r($breadcrumb);
    return $breadcrumb;
}
else {
      // This is our recursive function call.        
      $this->getparent($parentCat, $breadcrumb);
      }
}



Unable to return array from function - El Forum - 05-24-2009

[eluser]Dam1an[/eluser]
Hi, sorry it took me so long to respond (I bookmarked this thread intending to get back to it but forgot)
What are you doing in the controller after you've called it? I can't see anything fundamentally wrong with the function (although I would oersonally rework it a bit)


Unable to return array from function - El Forum - 05-24-2009

[eluser]TheFuzzy0ne[/eluser]
Ideally, your method should always return something other than void, like FALSE. Then you can test that you've actually got a result returned. If you add a die statement to the end of that method, I you may find it triggers sometimes. A more fluid code layout might look something like this:

Code:
function get_breadcrumb($id=NULL)
{
    $id = (int)$id;
    $breadcrumb = array();
    
    $res = $this->db->get_where('categories', array('id' => $id));
    
    if ($res->num_rows() > 0)
    {
        $row = $res->row_array();
        
        if ($row['parentid'])
        {
            $breadcrumb = array_merge($breadcrumb, $this->get_breadcrumb($row['parentid']));
        }
    }
    
    return $breadcrumb;
}

It's untested, but I think it might work.