leeming Posted November 8, 2006 Share Posted November 8, 2006 i have added print_r()'s and print() all over these functions, and i still dont understand why it is returning a wrong value...[code] function stackResearchTree($researchroot, $game = GAMEID) { /********************************************************** * Starts off the research tree, setting the root research * * returns the stack of researches * **********************************************************/ $tree_stack = array($researchroot); //sets up the stack, with the root research at the bottom return initTree($researchroot, $tree_stack); } function initTree($research, $tree_stack, $depth = 1) { /************************************************************** * Keeps calling its self untill all reserch needed, are found * **************************************************************/ //find req1? $req1 = researchReq($research); if(!in_array($req1, $tree_stack) && $req1 > 0) //not currently in array { array_push($tree_stack, "$req1"); //add req1 to the stack print_r($tree_stack); print"<BR>"; //call itself for req1 of req1 initTree($req1, $tree_stack, $depth + 1); } //else//if($req1 == 0) //has no requirement for req1 //{ //array_push($tree_stack, "$research"); //add req1 to the stack (end of the branch for this part) //} //else, do nothing, its already there /* do again for req2 */ //find req2? $req2 = researchReq($research, 2); if(!in_array($req2, $tree_stack) && $req2 > 0) //not currently in array { array_push($tree_stack, "$req2"); //add req2 to the stack print_r($tree_stack); print"<BR>"; //call itself for req2 of req2 initTree($req2, $tree_stack, $depth + 1); } //else//if($req2 == 0) //has no requirement for req1 //{ //array_push($tree_stack, $research); //add req1 to the stack (end of the branch for this part) //} //else, do nothing, its already there if($depth != 1) //still has more to loop { print"::$depth<BR>"; $depth --; return; } else { return $tree_stack; } } function researchReq($researchid, $req = 1, $game = GAMEID) { /************************************************************************** * Finds the id of the requirement, where $req is either 1 or 2, as there * * is only 2 research requirements * **************************************************************************/ $sql = "SELECT p".$req." FROM Research".gameTypeList("research", $game)." WHERE id='$researchid'"; $query = mysql_query($sql, cdb(3))or bavaderror($sql); $row = mysql_fetch_row($query); return $row[0]; }[/code]Basic understanding of this is, a research tree... only 2 nodes per parent (req1 and req2)stackResearchTree() - starts off the tree, so basically the rootresearchReq() - just finds out what the requirement is, (value of req1 or req2)initTree() - Builds up a stack, adding a unique id as it travels thru it.here are the results i get...[quote]Array ( [0] => 5 [1] => 2 )::2Array ( [0] => 5 [1] => 2 [2] => 4 )Array ( [0] => 5 [1] => 2 [2] => 4 [3] => 1 )::3Array ( [0] => 5 [1] => 2 [2] => 4 [3] => 1 [4] => 3 )::3::2::end[/quote]And the final return (from all of it) is[quote]Array ( [0] => 5 [1] => 2 [2] => 4 )[/quote]which is missing 2 parts...Is this because they are nested somehow? I can’t figure it out, as I’m returning the array, to use again to push a new value... Link to comment https://forums.phpfreaks.com/topic/26516-function-recursion-problem-solved-~-10-sane-ness-remaining-lol/ Share on other sites More sharing options...
btherl Posted November 8, 2006 Share Posted November 8, 2006 Perhaps you should do[code]$treestack = initTree($req1, $tree_stack, $depth + 1);[/code]Or alternatively, you can pass $treestack by reference. Link to comment https://forums.phpfreaks.com/topic/26516-function-recursion-problem-solved-~-10-sane-ness-remaining-lol/#findComment-121352 Share on other sites More sharing options...
leeming Posted November 8, 2006 Author Share Posted November 8, 2006 [quote author=btherl link=topic=114207.msg464666#msg464666 date=1162951647]Perhaps you should do[code]$treestack = initTree($req1, $tree_stack, $depth + 1);[/code]Or alternatively, you can pass $treestack by reference.[/quote]yes what luck eh... i solved it like a few minutes before you, after tearing my brain out all night... yes i was returning ther stack, but when i was calling the function i wasnt assigning it to any thing.. eg.. the stack its self.. *doh* Link to comment https://forums.phpfreaks.com/topic/26516-function-recursion-problem-solved-~-10-sane-ness-remaining-lol/#findComment-121355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.