Gen tree

Links for php scripts

Moderators: macek, egami, gesf

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

Post by 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.

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

Post by 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...

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests