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

[eluser]Ninjabear[/eluser]
This code doesn't seem to work. I don't think it's traversing the tree yet. When it reaches the nested call to recursion it passes in the whole array not just the section which needs to be traversed.

Also, the c_id is just a customer id and is not needed. The parent_id and p_id (project id) should be the only ids needed.

I tried this so far:

Code:
$Project->Children = $this->Recurs($Project, $Project->parent_id);

But I get an empty array. My understanding is that I should be passing the current project to check for child elements and also the current parent id but I just feel like there's a lot of code missing.

EDITED

I did it, but I nearly gave up computing altogether in the middle:

Code:
function Recurs($Projects, $ParentID = NULL)
  {
   $Ret = array();
   foreach($Projects as $Project)
   {
    if($Project->parent_id != $ParentID) // skip those rows not related to the parent
     continue;
    
    $Project->Children = $this->Recurs($Projects, $Project->p_id); // find any child rows related to this project
    $Ret[$Project->p_id] = $Project; // add the project to the
   }
   return $Ret;
  }


Thanks gRoberts.


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