Welcome Guest, Not a member yet? Register   Sign In
Unable to return array from function
#1

[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.
#2

[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
#3

[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);
      }
}
#4

[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)
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB