Welcome Guest, Not a member yet? Register   Sign In
Logic of recursive function
#1

[eluser]Ninjabear[/eluser]
I've been following this article on recursion in php.

I just can't get my head around the logic of recursion at the moment.

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.

This is Codeigniter 2.1.0.

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);
  }

The get_projects() function just returns a list of projects restricted by certain fields. I don't think the details matter too much except that it ends like this (by all means ask me if it would be helpful):

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;
}

So you get either a result or row plus num_rows in an array. get_projects() works throughout the site.

The problem is that wherever there is a child element it doesn't get printed.

Code:
Array
(
    [2] => Array
        (
            [0] => stdClass Object
                (
                    [p_id] => 120
                    [c_id] => 2
                    [project_name] => Another project
                    [project_desc] => another project
                    [date_added] => 2012-01-01 00:00:00
                    [parent_id] => 2
                    [allocated_hrs] => 100
                    [date_from] => 2012-01-01 00:00:00
                    [date_to] => 2012-01-01 00:00:00
                    [status] => active
                    [priority] => 1
                    [completed] => 1
                    [author] => 271
                    [p_date_added] => 1st Jan 2012
                    [date_from_formatted] => 2012-01-01
                    [date_to_formatted] => 2012-01-01
                    [proj_id] => 120
                    [cust_name] => Customer 2
                    [cust_id] => 2
                )

        )

    [3] => stdClass Object
        (
            [p_id] => 3
            [c_id] => 2
            [project_name] => This is a project
            [project_desc] => This is a projectThis is a projectThis is a projectThis is a projectThis is a projectThis is a projectThis is a projectThis is a projectThis is a project
            [date_added] => 2012-03-28 09:27:16
            [parent_id] => 1
            [allocated_hrs] => 100
            [date_from] => 2012-10-10 00:00:00
            [date_to] => 2012-11-10 00:00:00
            [status] => active
            [priority] => 1
            [completed] => 0
            [author] => 271
            [p_date_added] => 28th Mar 2012
            [date_from_formatted] => 2012-10-10
            [date_to_formatted] => 2012-11-10
            [proj_id] => 3
            [cust_name] => Customer 2
            [cust_id] => 2
        )

)

Notice here that p_id 2 isn't there but has child elements.


Messages In This Thread
Logic of recursive function - by El Forum - 03-30-2012, 01:11 AM
Logic of recursive function - by El Forum - 03-30-2012, 01:36 AM
Logic of recursive function - by El Forum - 03-30-2012, 02:21 AM
Logic of recursive function - by El Forum - 03-30-2012, 02:54 AM
Logic of recursive function - by El Forum - 03-31-2012, 05:26 AM
Logic of recursive function - by El Forum - 03-31-2012, 05:40 AM
Logic of recursive function - by El Forum - 03-31-2012, 07:25 AM
Logic of recursive function - by El Forum - 04-02-2012, 02:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB