Jump to content

Pass value from recursive function


everisk

Recommended Posts

Hello,

 

I have a recursive function which is running as expected. However, I dont know how could I access the variable in the recursive function outside of the function scope. The variables are in array. Below is my code. Thanks!

 

//get forwarder id
$sql = "select * from forward_network where email='$email'";
$tab = mysql_query($sql) or die("Cannot select email");
$row = mysql_fetch_array($tab);
$forwarder_id = $row['id'];

//find children of forwarder
$children = get_children($forwarder_id);
print_r($children);


function get_children($id) {
$sql = "select * from forward_network where parent_id=$id";
$tab = mysql_query($sql) or die("Cannot get children");

if($tab) {  //if there is value in $tab or children are found, then store it
	while($row = mysql_fetch_array($tab)) {
		$child['id'] = $row['id'];
		$child['email'] = $row['email'];
		$child['parent_id'] = $row['parent_id'];
		$child['level'] = $row['level'];
		//get children of children ,, recursive
		print "<pre>";
		print_r($child);
		print "</pre>";
		print "<br>";
		get_children($row['id']);
	}
}

	return $child;

}

Link to comment
https://forums.phpfreaks.com/topic/78203-pass-value-from-recursive-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.