Hi Ive created this recursive function that buildes a tree from a database,
id, name, _parent
<?
$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);
$foo = $cnt-1;
echo " ".$row['name']."<br>";
buildTree($row['id'], $cnt + 1);
}
}
?>
<?
buildTree();
?>
The out put is this
» Hjem
» Produkter
» » Radioer
» » Tv-er
» » Videoer
» Om oss
» » Vår vision
» » Historie
» » » Gammel
» » » Moderne
» » Partnere
» » Ledige stillinger
» FAQ
» » Produkter
» » bla bla ...
» » Priser
» Kontakt
» » Salg
» » Support
» » Webmaster
ok, simple.
But how can I for each item(name) get the parent ids
so the output looks like this
2 Om oss
2_1 Vår vision
2_2 Historie
2_2_1 Gammel
2_2_2 Moderne
2_3 Partnere
2_4 Ledige stillinger
Can someone please help me, have been searching on the subject and tried coding on this for some days, but I cant get it anything to work, please.
thanks in advance
Thomas