Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Recursion problem - value returns null... variable scope issue?

Status
Not open for further replies.

TheDust

Programmer
Aug 12, 2002
217
US
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:

nested_folders.gif"


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"]);
	}
}

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 ID
echo "value returned: ".$parentID."<br>"; // for troubleshooting

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: 135
value returned:

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...
 
can i try a shot in the dark here? what happens when you change the line to:

Code:
[red]return [/red]findParentID($userID, $Row["parentID"]);



*cLFlaVA
----------------------------
[tt]( <P> <B>)[sup]13[/sup] * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
a couple of things:

1. you are not testing for error conditions. if your query returns an error (which i think it might, see 2) then $Row will be populated and your conditional will be passed. but it is populated with an error object rather than an array of values.

2. but i suspect the real issue is that this line:
Code:
$SQL = "SELECT parentID FROM folders WHERE folderID=".$folderID;
could it be that the table name here should be navAccessFolders?

If not, then i think i'd need to see the table structures for the two tables. your code looks prima facie ok, so it's down to logic errors, table design issues or my own blindness.
 
cFlaVA hit it on the head. I figured that the function continuously called upon itself until the condition was true and then it returned that value. I didn't think I had to add a "return" line in the else statement. Well done! Thank you SO much! No more banging my head against the wall today! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top