Jump to content

recursive functions in recursive function


lopolla

Recommended Posts

I have a recursive function which is calling a recursive funtion. The 2nd recursive function have a weird issue, because when I use return-statement it returns to the function itself (probably because that is where it is originated) - so any suggestions to how I secure coming back to main function?

 

Here is the code I am currently working on........

 

 

function display_children($arrIn)
{
    global $arrContentGlobal;

    # retrieve all children of $parent
    $sql1 = "
    SELECT * FROM formulaTree
    WHERE versionId='$arrIn[versionId]'
    AND criterionId='$arrIn[criterionId]'
    AND formulaTreeIdRef='$arrIn[parentId]'
    ";
    $res1 = mysql_query("$sql1");
    $num1 = mysql_num_rows($res1);

    while($obj = mysql_fetch_array($res1))
    {
        # Find level -> run other function (THIS IS HERE PROBLEM STARTS.....)
        $arrInLevel[formulaTreeId] = "$obj[formulaTreeId]";
        $arrLevel = displayFathers($arrInLevel);

        # Go for this function again............
        $arrIn[parentId] = "$obj[formulaTreeId]";
        display_children($arrIn);
    }
}

 

 

function displayFathers($arrIn)
{
    if(!$arrIn[level])
    {
        $arrIn[level] = 0;
    }

    $sqlChild = "SELECT * FROM formulaTree WHERE formulaTreeId='$arrIn[formulaTreeId]'";
    $resChild = mysql_query("$sqlChild");
    $objChild = mysql_fetch_array($resChild);

    $arrConceptsId[] = "$objChild[formulaTreeIdRef]";

    if($objChild[formulaTreeIdRef] != "0")
    {
        $arrIn[level]++;
       
        $arrIn[formulaTreeId] = "$objChild[formulaTreeIdRef]";
        displayFathers($arrIn);
    }

    $arrOut[level] = $arrIn[level];
    return($arrOut);
}

 

 

 

Link to comment
Share on other sites

A child can have more than one father?   If you need get the grandfathers too.... I'd set up something like this

 

 

function getFather($child, array &$fathers, $withAncestors = false){

       // $child is the child we will get the father for and if $withAncestors is true we will also get the grandfathers and their grandfathers

       // $fathers is an array that we pass in by referece. we will add the father(s) here as we get them!

       // $withAncestors   this WILL initiate the recursiveness

 

       if( ! $child ) return false;  // no child, no father

       /* select the father from the database and plop it into $father, if no father in the database return false */

 

       $sqlChild = "SELECT * FROM formulaTree WHERE formulaTreeId={$child}";

       $rslt = mysql_query("$sqlChild");

       while($father = mysql_fetch_assoc($rslt)){

          $fathers[] = $father;

           if($withAncestors) getFather($father['id'], $fathers, true);   // recursive get the father of the father 

       }

       return false;

}

 

$child = 10; // the child id for the father we want to get

$fathers = array(); // the list of fathers. empty right now

 

getFather($child, $fathers, true);  // gets the father, and father's father, and father's father's father.

 

var_dump($fathers);   // dumps all of the fathers in the family tree.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.