I've been trying to do a recursive function and I just can't get my head around the logic of it. I've been reading an article on PHP recursion but can't seem to make it work myself.
Basically I have a nested tree of projects with infinite levels. So 1 is the root node and then there are others beneath it. Each one has a p_id (primary key) and parent_id to show who its parent is.
P.s. This is Codeigniter 2.1.0 with MySQL.
The get_projects function just returns a list of projects restricted by certain fields. I don't think the details matter except that it ends like this:PHP Code:function recursion($p_id)
{
$projects = $this->get_projects( array('parent_id'=>$p_id) );
if ($projects['num_rows']>0)
{
foreach ( $projects['result'] as $k =>$v )
{
$children = $this->get_projects( array('parent_id'=>$v->p_id) );
if ( $children['num_rows'] > 0 )
{
$array[ $v->p_id ] = $this->recursion( $v->p_id );
}
elseif (isset($v->p_id))
{
$array[] = $v;
}
}
}
return (isset($array) ? $array : false);
}
So you get either a result or row plus num_rows in an array. It works throughout the site.PHP Code:$query = $this->db->get();//query
if(isset($options['p_id']))
$result['result'] = $query->row();
else
$result['result'] = $query->result();
$result['num_rows'] = $query->num_rows();
return $result;
}
Error
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 18 bytes) in /home/seedetai/public_html/simple_collab/system/database/DB_active_rec.php on line 84
I don't think this should be happening I think it's a logic issue with the code. I haven't tried increasing the memory limit but I'll do that if I can get the code right.




Reply With Quote
