TreeView - Finally a working tree view function to be used as you want. Simple create the Table using the code provided and you will be able to have a tree view in your project. Download the zip to get the images.
Run this script to create the treeview table
You can change it to suit your needs (like adding
types of elements such as files or diferent types
of directories).
#Starts here
CREATE TABLE TAB_TREEVIEW (
treeview_cod int(11) NOT NULL auto_increment,
treeview_name text NOT NULL,
treeview_desc text NOT NULL,
treeview_parent_cod int(11) NOT NULL default '-1',
PRIMARY KEY (treeview_cod)
) TYPE=MyISAM;
INSERT INTO TAB_TREEVIEW VALUES (2, 'parent1', '', -1);
INSERT INTO TAB_TREEVIEW VALUES (3, 'parent2', '', -1);
INSERT INTO TAB_TREEVIEW VALUES (4, 'parent3', '', -1);
INSERT INTO TAB_TREEVIEW VALUES (5, 'child1', '', 2);
INSERT INTO TAB_TREEVIEW VALUES (6, 'child2', '', 2);
INSERT INTO TAB_TREEVIEW VALUES (7, 'grandchild1', '', 5);
INSERT INTO TAB_TREEVIEW VALUES (8, 'grandchild2', '', 5);
INSERT INTO TAB_TREEVIEW VALUES (9, 'child3', '', 4);
INSERT INTO TAB_TREEVIEW VALUES (10, 'child4', '', 4);
INSERT INTO TAB_TREEVIEW VALUES (11, 'grandchild3', '', 9);
INSERT INTO TAB_TREEVIEW VALUES (12, 'grandgrandchild1', '', 11);
INSERT INTO TAB_TREEVIEW VALUES (13, 'grandgrandchild2', '', 11);
#Ends here
Change the variables bellow to fit your system.
I suggest you create a db.inc.php file an include it
instead of edit every page every time you change any
variable.
//Function used by the other bellow recursively to help to check
//if the folder that`s being opened is son of the one being displayed.
function checkParents($treeview_cod,$treeview_current,$treeview_parents)
{
$sql="select * from TAB_TREEVIEW where treeview_cod=".$treeview_cod." order
by
treeview_name";
$rs=mysql_query($sql);
if (mysql_num_rows($rs)!=0)
{
while($rows=mysql_fetch_array($rs))
{
$treeview_parents[]=$rows["treeview_cod"];
checkParents($rows
["treeview_parent_cod"],$treeview_current,&$treeview_parents);
}
}
}
//Function used to check if the folder that`s being opened
//is son of the one being displayed.
function openFolder($treeview_cod,$treeview_current)
{
checkParents($treeview_cod,$treeview_current,&$treeview_parents);
if (in_array($treeview_current,$treeview_parents))
{
return true;
}
}
//This was the most difficult of all the functions cause it
//calculates and organize the hierarchy of the opened folders...
//A pain in the ass, literally. I had night mares and woke up
//in the middle of the night because of this son of a b*
function drawBlanksIntersecs($treeview_cod)
{
$sql="select * from TAB_TREEVIEW where treeview_cod=".$treeview_cod;
$rs=mysql_query($sql);
$row=mysql_fetch_array($rs);
$sql2="select * from TAB_TREEVIEW where treeview_cod=".$row
["treeview_parent_cod"];
$rs2=mysql_query($sql2);
$row2=mysql_fetch_array($rs2);
$sql3="select * from TAB_TREEVIEW where treeview_parent_cod=".$row2
["treeview_parent_cod"]." order by treeview_name";
$rs3=mysql_query($sql3);
$i=0;
if (mysql_num_rows($rs3)!=0)
{
while ($row3=mysql_fetch_array($rs3))
{
//Function that builds the tree from root to the last opened folder
//I`m very proud of it, `cause it works!!! Don`t care if it is too
//messy or has too many ifs. At least idention is right, so if you
//want go ahead and fix it yourself.
function buildTreeView($action,$treeview_cod=0,$treeview_parent_cod=-1)
{
$sql="select * from TAB_TREEVIEW where
treeview_parent_cod=".$treeview_parent_cod." order by treeview_name";
$rs=mysql_query($sql);
if (mysql_num_rows($rs)!=0)
{
$i=1;
while ($parent=mysql_fetch_array($rs))
{
echo "<tr valign=top>";
echo "<td nowrap>";
drawBlanksIntersecs($parent["treeview_cod"]);
$sql2="select * from TAB_TREEVIEW where
treeview_parent_cod=".$parent
["treeview_cod"]." order by treeview_name";
$rs2=mysql_query($sql2);
if (mysql_num_rows($rs2)!=0)
{
if ($action=="expand"&&openFolder($treeview_cod,$parent
["treeview_cod"]))
{
echo "<a href=treeview.php?
action=expand&treeview_cod=".$parent
["treeview_parent_cod"].">";