CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Recursion

  1. #1
    Join Date
    Dec 2004
    Posts
    438

    Question Recursion

    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.

    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'] > )
                {
                  
    $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 except that it ends like this:

    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;
        } 
    So you get either a result or row plus num_rows in an array. It works throughout the site.

    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.
    Last edited by PeejAvery; March 29th, 2012 at 03:54 PM. Reason: Changed code tags to PHP tags

  2. #2
    Join Date
    Dec 2004
    Posts
    438

    Re: Recursion

    I've now fixed the memory issue because I found a problem where the root element - 1 was linking to itself so I changed it to null. It now prints the right data except wherever there are child elements it doesn't print that element. So if p_id = 2 has 2 child elements p_id 2 itself won't appear in the array:

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured