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

Thread: Recursion

Threaded View

  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

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