thedust2010 Posted June 29, 2006 Share Posted June 29, 2006 In my 3 years of PHP application development I don't think I've ever been this perplexed. It is rather difficult to explain, but hopefully it makes sense. For brevity I'm not giving details about the database table structures. Really it is irrelevant for what my problem is, which appears to be some sort of variable scope issue. The problem appears to be something really elementary...I have a database-driven system of files and folders with permission to view these contents specified on a per-user basis. Occasionally this means assigning a folder a new parent folder if the user is blocked from seeing it. I've provided a visual aid that makes this much easier to comprehend:[img src=\"http://dustwurks.com/nested_folders.gif\" border=\"0\" alt=\"IPB Image\" /]So here in this example we would be looking to assign "TEST2" as the parent of "INSIDE 4". To do this, I have a recursive function that finds the new parent ID that should be assigned. It looks like this:[code]function findParentID($userID, $folderID) { global $DB; $SQL = "SELECT folderID FROM navAccessFolders WHERE folderID=".$folderID." AND userID=".$userID; $Row = $DB->GetRow($SQL); if ($Row) { echo "value to return: ".$folderID."<br>"; // for troubleshooting return $folderID; } else { $SQL = "SELECT parentID FROM folders WHERE folderID=".$folderID; $Row = $DB->GetRow($SQL); findParentID($userID, $Row["parentID"]); }}[/code]Now let's say the folder ID for "INSIDE 4" is 138. To get the parentID I would use:[code]$parentID = findParentID(1, 138); // using "1" as a sample user IDecho "value returned: ".$parentID."<br>"; // for troubleshooting[/code]This functionality WORKS if the findParentID function never calls upon itself again. Then the value returns null. But here is the part I don't think anyone can explain to me. The actual output from the code used above is:[code]value to return: 135value returned: [/code]I am echoing the value just before the function returns it and it IS in fact the correct value. However, it is not returning the value that I am seeing in the output. It returns null. How can this be? There is not one line of code between the echo and the return. And again, this ONLY is happening when the function calls upon itself again. The function works but the value returns null anyways.If someone can figure this one out, you will have made my list of all-time top coders (and maybe we can hire you for some work sometime??). Any advice or help is much appreciated. I'm so close... Quote Link to comment https://forums.phpfreaks.com/topic/13257-php-recursion-problem-value-returns-null-variable-scope-issue/ Share on other sites More sharing options...
nogray Posted June 29, 2006 Share Posted June 29, 2006 I think you are missing a return in the else part.return findParentID($userID, $Row["parentID"]);this just a guess, but give it a shot. Quote Link to comment https://forums.phpfreaks.com/topic/13257-php-recursion-problem-value-returns-null-variable-scope-issue/#findComment-51040 Share on other sites More sharing options...
Guest Dustin Hansen Posted June 30, 2006 Share Posted June 30, 2006 That COMPLETELY did it. I didn't realize I had to have the "return" there in the else statement. I figured it just recursively looped until it was true and then returned that value. I'll need to wrap my head around this one. Thanks very very much!! Quote Link to comment https://forums.phpfreaks.com/topic/13257-php-recursion-problem-value-returns-null-variable-scope-issue/#findComment-51422 Share on other sites More sharing options...
nogray Posted June 30, 2006 Share Posted June 30, 2006 a recursive is just like any other function instead it calls itself. So basically it works kinda like thisfunction, return match if true, return function if false, return match if true return function if false. etc etc....so, after looping so and so times, each function call must return a result until it reach the top function which will return the final result. A bit odd at first, but it's simple when you get it. Quote Link to comment https://forums.phpfreaks.com/topic/13257-php-recursion-problem-value-returns-null-variable-scope-issue/#findComment-51434 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.