Page 1 of 1

[SOLVED] help retrieve value from function

Posted: Fri Mar 09, 2007 7:46 pm
by Think Pink
Hello, pls advice.
I have this function

Code: Select all

function children($parent) {
	global $database;

	$result = $db->query("SELECT * FROM tree WHERE (parentId = '".$parent."')");

	while ($row = $result->fetch()) {
		@extract($row);

		echo $treeId;
		children($treeId);
		// call this function again
		// child's children
	}
}
i am interested retrieving the $treeId from this function and assign it's value to something else.
if I use

Code: Select all

return $treeId;
insted of

Code: Select all

echo $treeId;
the function stops executing, but if I use

Code: Select all

echo $treeId;
the value it's displayed and i don't want that.
Any ideeas?
Thx

Posted: Fri Mar 09, 2007 8:20 pm
by John Cartwright
I think what you want it

Code: Select all

return children($treeId);

Posted: Fri Mar 09, 2007 8:30 pm
by Think Pink
I think what you want it
Nope

later in my code I need to do

Code: Select all

$newVar = children($id);
and for this I need to get the $treeId from that function but in the same time I need that function to execute recursevly.

Posted: Fri Mar 09, 2007 8:38 pm
by feyd
I would suggest adjusting the table schema so it is suited for trees more. Specifically, the various hierarchical structures referred to in various threads.

Posted: Fri Mar 09, 2007 8:53 pm
by Think Pink
I'm affraid I don't understand what exactly do you mean, but i'll explain a little what i want to do, maybe this will help.

I have 2 tables.

short example
tree
treeId | parent | Name
1 | | Cuisine
2 | 1 | Chinese
3 | 2 | East China
4 | 2 | West China

and

books
id | tree | BookName | Author
1 | 3 | Some book | Jim

now, I want to delete the record with treeId = 2 from tree table.
Because this is parent for other records in the tree table I need to delete them recursively.
I need that $treeId from my function because I need to check if that $treeId is refference in books table. If it is, then the book that has tree would be deleted.
Hope you understand what I mean.

[SOLVED] help retrieve value from function

Posted: Fri Mar 09, 2007 9:07 pm
by Think Pink
well, seems like I'm complicating things.
I inserted my other code into that function and now works great.
thx for your replyes