Click to See Complete Forum and Search --> : Tree script
thoand
01-06-2004, 09:00 PM
Hi here are a litle recursive script that builds a tree like:
name
-name
--name
---name
-name
--name
-name
--name
---name
... and so on.
It takes data from a db structure like this
id int(9) auto_increment ... uniqe id
name char(255) ... name to be printed
_parent int(9) ... holds the parent id (id)
<?
$link = mysql_connect("localhost", "", "");
mysql_select_db("test");
?>
<?
function buildTree($id = 0, $cnt = 1) {
$query = "SELECT * FROM names WHERE _parent = ".$id."";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo str_repeat("*",$cnt);
echo $row['name']."<br>";
buildTree($row['id'], $cnt + 1);
}
}
?>
<?
buildTree();
?>
Ypu can ofcourse modify it to suit your needs,
what do you think?
best regards
Thomas A
thoand
01-06-2004, 09:01 PM
all top level names has to have 0 as _parent
Moonglobe
01-07-2004, 12:59 AM
set the query to "SELECT * FROM names WHERE _parent in ( ".$id.",null)" and you can get around that.
Moonglobe
01-07-2004, 01:03 AM
and it'd be nice to be able to specify what character to use instead of "*" all the time...
Weedpacket
01-07-2004, 01:26 AM
Originally posted by Moonglobe
and it'd be nice to be able to specify what character to use instead of "*" all the time... Like, select the fields you actually want?
Moonglobe
01-07-2004, 01:27 AM
that'd be nice too, but i meant that you could use different chars for indenting.... maybe per-level if you passed an array, so it would rotate.
Weedpacket
01-07-2004, 07:35 AM
Originally posted by Moonglobe
that'd be nice too, but i meant that you could use different chars for indenting.... maybe per-level if you passed an array, so it would rotate. More flexible perhaps, a <span> style based on the depth of node ( "treelevel1", "treelevel2", ... etc.).
thoand
01-07-2004, 09:43 AM
Hi folks,
thanks for the input,
best regards,
Thomas
drawmack
01-08-2004, 02:57 AM
I was bored so attached you'll find a script that will test to see if your environment can run and the script and if not configure your environment to do so.
the changes are listed here:
/* CHANGE LOG
0) Changed _parent column to parent column - I made a boo boo on table creation
and this change was easier then that fix.
1) Add parameter $idnt to allow the user to specify indentation
2) Changed default $cnt to 0 so that user is not forced into having an
indent on the root level
3) added mysql_free_result to clean up after self
4) added parameter checking
a) added parameter $running so we know if we have to do parameter checking
5) return an array instead of echoing the tree to allow more flexibility
*/
function buildTree($id = 0, $cnt = 0, $idnt = '*', $running = 0) {
//if we are not in a recursive loop
if(!$running || !is_numeric($running)) {
if(!is_numeric($id)) {return -1;}
if(!is_numeric($cnt)) {return -1;}
if(!is_numeric($cnt)) {return -1;}
} //end if
$retVal = array();
$query = "SELECT * FROM names WHERE parent = ". $id;
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
array_push($retVal,(str_repeat($idnt,$cnt) . $row['name']));
$retVal = array_merge($retVal,buildTree($row['id'], $cnt + 1,$idnt,1));
}
mysql_free_result($result);
return($retVal);
} //end build tree
thoand
01-08-2004, 06:44 AM
Thanks for the interest,
but one question,
I thought mysql_free_result had no effect anymore, that PHP always cleans up.
Thomas
drawmack
01-08-2004, 10:06 AM
never trust someone else's code to clean up after yours
Shrike
01-09-2004, 08:08 AM
Originally posted by thoand
Thanks for the interest,
but one question,
I thought mysql_free_result had no effect anymore, that PHP always cleans up.
Thomas
PHP frees results when script execution has ended, mysql_free_result() can free up the memory beforehand, useful if you have a huge recordset slowing everything down.
tokeiito
12-21-2005, 01:40 PM
nice code, i've wrote smth like that too, but it's alitle bit longer :)
but now i have problem with identifying subNode's. exmpl:
node
-node
--node
-node
node
-node
--node
---node
----node
and so on...
my dhtml meniu use <ul><li></li></ul> for node. exmpl:
<ul>
<li>node</li>
<li>node
<ul>
<li>node2</li>
</ul>
</li>
</ul>
and know i'm stuck. how to identify where is node with subNode or, subNode with subSubNode and so on...
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.