Gen tree

Links for php scripts

Moderators: macek, egami, gesf

CryptoKnight
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Jan 28, 2003 5:37 am

Gen tree

Postby CryptoKnight » Tue Jan 28, 2003 5:51 am

Hey, I've got a little problem

I'm doing a hp for my family and I would like to put a Genealogy tree (is that the right word??) on it. In my database, I have a table of persons, a table of parent/child-relationships and a table of married couples.

I'm thinking about som recursive stuff with som tables, but that's as far as my imagination goes. I can't see how to do it...

To give an idea of what I think it should look like, I temporarily put out an image: http://moorhuhn3.starthotel.dk/stamtrae.jpg
(text is in danish, but you should get the idea...)

Does anybody happen to have a function like that lying around, or do you have an idea on how to make it?

/CK

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Tue Jan 28, 2003 11:53 pm

Code: Select all

<?php
class Tree
{
   var $host, $user, $pass, $db, $table, $dbconnection;


function set_db($host, $user, $pass, $db, $table)
{
  $this->host = $host;
  $this->user = $user;
  $this->pass = $pass;
  $this->db = $db;
  $this->table = $table;


}
function get_parent($id)
{

$query = 'select parentid from '.$this->table .' where id = "'.$id.'" order by name asc';
$result = $this->query($query);
$myrow = mysql_fetch_array($result);
return $myrow['parentid'];

}
function is_root($id)
{
   if($this->get_parent($id) == 0)
   {
     return 1;
   }
   else
   {
     return 0;
   }
}

function get_children($id)
{
        $query = "SELECT id, name FROM $this->table WHERE parentid = '$id' order by name asc";
        $result = $this->query($query);
        $count = 0;
        while ($row = mysql_fetch_array($result))
        {
               $children[$count]["id"] = $row["id"];
               $children[$count]["name"] = $row["name"];
               $count++;
       }
         return $children;
}
function get_type($id)
{
       if($this->get_children($id)        )
         {
                 return 1;
        }
        else
       {
               return 0;
       }
}
function get_name($id)
        {
                $query = "SELECT name FROM $this->table WHERE id = '$id'";
                $result = $this->query($query);
                $row = mysql_fetch_row($result);
                return $row[0];
        }
function get_description($id)
        {
                $query = "SELECT description FROM $this->table WHERE id = '$id'";
                $result = $this->query($query);
                $row = mysql_fetch_row($result);
                return $row[0];
        }

 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;
                $this->ancestors[$count]["name"] = $this->get_name($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);
                }
        }


function query($query)
{
     if(!isset($this->dbconnection))
    $this->dbconnection = mysql_connect($this->host, $this->user, $this->pass)
         or die ("Cannot connect to database");
        // run query
      $ret = mysql_db_query($this->db, $query, $this->dbconnection)
         or die ("Error in query: $query");
        // return result identifier
      return $ret;

}
function print_menu_tree($id = 0)
        {
                $result = $this->get_children($id);
                echo "<ul>";
                for ($x=0; $x<sizeof($result); $x++)
                {
                        if($this->get_type($result[$x]["id"]))
                        echo $result[$x]["name"];
                        else
                        echo "<li>" . $result[$x]["name"] . "[" . $result[$x]["id"] . "]";
                        $this->print_menu_tree($result[$x]["id"]);
                }
                echo "</ul>";
        }
}
?>


here's a sample file

Code: Select all

<?php
$obj = new Tree();
$obj->set_db($db_host,$db_user,$db_pass,$database,'geneology');
// print the entire tree
$obj->print_menu_tree(0);
?>


just make sure in your db you have the fields id, parentid and name and this should work. Hope it helps. I use it so much I don't know what I'd do without it.

CryptoKnight
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Jan 28, 2003 5:37 am

Postby CryptoKnight » Wed Jan 29, 2003 3:57 am

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 :o

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

CryptoKnight
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Jan 28, 2003 5:37 am

Postby CryptoKnight » Wed Jan 29, 2003 4:57 am

never mind the last. I thought something up myself... not pretty but it works:

===================
function print_menu_tree($id = 0){
$start_id = 55;
if($id){
$result = $this->get_children($id);
for ($x=0; $x<sizeof($result); $x++)
{
echo "<table cellspacing=0 cellpadding=0><tr><td width=50></td>";
if($x<sizeof($result)-1){
echo "<td background='streg.gif'><img src='streg'></td>";
}
else{
echo "<td valign='top'><img src='streg' width=1 height=7></td>";
}
echo "<td valign='top'><img src='vandret.gif' height=15 width=20></td><td>";
echo "<a href='person.php?id=".$result[$x]["id"]."'>". $this->get_name($result[$x]["id"])."</a>";
$this->print_menu_tree($result[$x]["id"]);
echo "</td></tr></table>";
}
}
else{
echo "<table cellspacing=0 cellpadding=0><tr><td>";
echo "<a href='person.php?id=$start_id'>" . $this->get_name($start_id) . "</a>";
$this->print_menu_tree($start_id);
echo "</td></tr></table>";

}
}

------------------
$obj->print_menu_tree();

===========================

As you might notice, if put the whole thing in tables and added som graphics: 'streg.gif' is a 1x1 black pixel and 'vandret.gif' is a 15px high and 1px wide image with a blak pixel in the middel, rest is transperent. You can see a preview of it here: Image

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Fri Jan 31, 2003 1:05 am

gald it worked for ya... the print_tree method is really just an example on how to print the whole tree...


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 0 guests

cron