lopolla Posted November 24, 2013 Share Posted November 24, 2013 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);} Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 24, 2013 Share Posted November 24, 2013 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. Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 24, 2013 Share Posted November 24, 2013 Also, a set a children should not have more than 1 father. So you don't need to call getFather for each child...... Unless, you descend into a child's children set..... but at this point you ALREADY know the father if you handle it properly =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.