Thanks! After just a few modifications I could use the script perfectly :-) BUT... just a little itty-bitty problem: how do I get the person I'm writing the tree over to show up in top of the tree? ("$obj->print_menu_tree(55);" if I'm writing the tree for person 55)
I'm just gonna post the code, so you can see my modifications:
===================
<?php
class Tree
{
var $host, $user, $pass, $dbconnection;
function set_db($host/*, $user, $pass */)
{
$this->host = $host;
// $this->user = $user;
// $this->pass = $pass;
}
//----------------------------
// Find Personnens Foraelder
//----------------------------
function get_parent($person_id)
{
$query = 'select foraelder from foraelder_barn where barn = $person_id';
$result = $this->query($query);
$myrow = mysql_fetch_array($result);
return $myrow['foraelder'];
}
//------------------------------
// Find personens Børn
//------------------------------
function get_children($person_id)
{
$query = "SELECT barn FROM foraelder_barn WHERE foraelder = '$person_id'";
$result = $this->query($query);
$count = 0;
while ($row = mysql_fetch_array($result))
{
$children[$count]["id"] = $row["barn"];
$count++;
}
return $children;
}
//--------------------------------
// Har denne person nogen børn?
//--------------------------------
function get_type($id)
{
if($this->get_children($id) )
{
return 1;
}
else
{
return 0;
}
}
//----------------------------------
// Find personnens Navn
//----------------------------------
function get_name($id)
{
$query = "SELECT navn FROM navne WHERE id = '$id'";
$result = $this->query($query);
$row = mysql_fetch_row($result);
return $row[0];
}
//-------------------------------
// find forfædre
//--------------------------------
function get_ancestors($id, $count = 0)
{
// get parent of this node
$parent = $this->get_parent($id);
// if not at the root, add to $ancestors[] array
if($parent)
{
$this->ancestors[$count]["id"] = $parent;
// recurse to get the parent of this parent
$this->get_ancestors($this->ancestors[$count]["id"], $count+1);
// all done? at this stage the array contains a list in bottom-up order
// reverse the array and return
return array_reverse($this->ancestors);
}
}
//---------------------------------
// databasekald
//---------------------------------
function query($query)
{
if(!isset($this->dbconnection))
$this->dbconnection = mysql_connect($this->host/*, $this->user, $this->pass */)
or die ("Cannot connect to database");
mysql_select_db("familie",$this->dbconnection);
// run query
$ret = mysql_query($query)
or die ("Error in query: $query");
// return result identifier
return $ret;
}
//-------------------------
// Skriv træet
//-------------------------
function print_menu_tree($id = 0)
{
$result = $this->get_children($id);
echo "<ul>";
for ($x=0; $x<sizeof($result); $x++)
{
echo "<a href='person.php?id=".$result[$x]["id"]."'>". $this->get_name($result[$x]["id"])."</a>";
$this->print_menu_tree($result[$x]["id"]);
}
echo "</ul>";
}
}
?>
<?php
$obj = new Tree();
$obj->set_db('localhost',$db_user,$db_pass);
// print the entire tree
$obj->print_menu_tree(55);
?>
=================
- hope I didn't mess up too much
Anyway... if I write the name before I call write_menu_tree() the name floats over the tree, looking really un-natural and if I write it inside write_menu_tree() it prints everytime... what should I do??
besides that: Thanks a lot! it really helped me out !
/CK