What do you mean by it fails? Any particular error, or does it just time
out?
I've made a different recursive function to print a tree-menu, you can have
a look at it if you want but it might not be what you're after. It prints a
tree-menu, and reqires the table to have at least a field named id and
parent. parent telling which node an object is connected to.
But I guess you know that already, I just got carried away! Maybe someone
could use this.
good night!
:-)
PHP4:
This script makes just one query to the server, fetching the id, parent and
name of each object, so u don't have to make multiple queries. Don't know if
it is any faster, though.
<?
function Recurse( $parent, $level=0, $item_string=0 ) {
global $expand, $obj;
$tmp = $obj; reset( $tmp );
foreach( $tmp as $k=>$v ) {
if( $v->parent == $parent ) {
(in_array( $v->id, $expand))?$X="> ":$X=" ";
echo @str_repeat( " ", $level*3 ).
"$X<a
href='$PHP_SELF?item=".$item_string.",".$v->id."'>$v->item</a><br>";
if ( in_array( $v->id, $expand ) ) recurse( $v->id, $level +1,
$item_string.",".$v->id );
}
}
}
// id = the key, item=the name of an object, and parent=the connected node.
ord is just the order of objects (which i've set in the db)
$sql = "select id,item,parent from menu order by parent, ord";
$qry = mysql_query( $sql );
while( $tmp = mysql_fetch_object( $qry )) { $p = (int) $tmp->id+0; $obj[$p]
= $tmp;}
$expand = explode( ",", $item ); // expand is an array containing the nodes
to be opened
//(check the URI :-)
Recurse(0); // start with top-level objects (starting node)
?>
----- Original Message -----
From: "Peter Van Dijck" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, March 10, 2001 9:43 PM
Subject: [PHP] objects in functions?
> Hi,
> I want to make a recursive function generating a hierarchy using a class.
>
> The following code works fine when I don't wrap the function around it,
> but when I do it fails. I can't figure out why...
> I'm thinking I may need to make some vars global, or maybe you can't use
> objects inside functions? (No, couldn't be)
>
> Here's the code:
> .........
> $root = $tree->open_tree ($title, "");
>
> function generate_nodes($id) {
>
> $sql = "select * from hierarchy where parent = $id";
> $res = mysql_debug_query($sql);
> while ($row = mysql_fetch_array($res)) {
> $node = "node" . $count;
> $$node = $tree->add_folder ($root, $row["name"] . " <font
> size=1>" . $row["id"] . "</font>", "");
>
> $count++;
> }
> }
> generate_nodes($id);
> ........
> Thanks for any hints...
> Peter
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> http://liga1.com: building multiple language/culture websites
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]