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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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