This topic is locked

Treeview

9/19/2008 9:39:08 AM
PHPRunner General questions
J
jbujosa author

Can anobody tell me how to output a treeview of MySQL table. I mean the displyed graph should look like windows explorer way of displaying directory files
Thank you

T
thesofa 9/20/2008

If you want to see each database and all the different tables and views in it, try Navicat for mySql

there is a free 30 day trial here, i have used it for years and I keep up with the updates, it is a fantastic tool.

W
wypman 9/20/2008

I'll second that. Very easy to use as well.

J
jbujosa author 9/21/2008

If you want to see each database and all the different tables and views in it, try Navicat for mySql

there is a free 30 day trial here, i have used it for years and I keep up with the updates, it is a fantastic tool.


Thank you;
But I wish to show the tree in a page built with PHPrunner, for example for a hierarchy of products. Perhaps it can be shown as a table with an apropiate query. no?

N
nix386 9/22/2008

Given the above responses I would be thinking no....

J
jje 9/23/2008

Agree, it would be nice to have a treeview function in phprunner. Anyway a find the following php code on the net, but didn't tested yet. Maybe it would be usefull to you.
<?
/

THIS IS THE PHP_TREEVIEW

Made by Pablo Santiago Sanchez

Copyright Pablo Santiago Sanchez

<phackwer@hotmail.com>

DON'T REMOVE THOSE CREDITS!!!

You may remove any other comment.

/
/*

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.
/
//Connection to database
define("PASSWORD","");

define("HOST","localhost");

define("USER","root");
$LINK=mysql_connect(HOST,USER,PASSWORD);

mysql_select_db("test",$LINK);
//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;

if ($rs=mysql_query($sql))

$row=mysql_fetch_array($rs);
$sql2="select from TAB_TREEVIEW where treeview_cod=".$row["treeview_parent_cod"];

if ($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_query($sql3))

{

while ($row3=mysql_fetch_array($rs3))

{
$i++;
if ($row3["treeview_cod"]==$row["treeview_parent_cod"] && mysql_num_rows($rs3)==$i)

{

$is_last=1;

}

else

{

$is_last=0;

}

}

}
if ($row["treeview_parent_cod"]!=-1)

{

drawBlanksIntersecs($row["treeview_parent_cod"]);
if ($is_last==1)

{

echo "<img src=images/trv_blank.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

else

{

echo "<img src=images/trv_nointersec.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

}
}
//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,$depth=0,$parent_last=0)

{
$sql="select from TAB_TREEVIEW where treeview_parent_cod=".$treeview_parent_cod." order by treeview_name";

if ($rs=mysql_query($sql))

{

$depth++;

$i=1;

while ($parent=mysql_fetch_array($rs))

{

echo "<tr valign=top>";

echo "<td nowrap>";
if ($parent["treeview_parent_cod"]!=-1)

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"].">";
if (mysql_num_rows($rs)==$i)

{

echo "<img src=".$dir."images/trv_intersecminus_end.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

else

{

echo "<img src=".$dir."images/trv_intersecminus.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

}

else

{

echo "<a href=treeview.php?action=expand&treeview_cod=".$parent["treeview_cod"].">";
if (mysql_num_rows($rs)==$i)

{

echo "<img src=".$dir."images/trv_intersecplus_end.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

else

{

echo "<img src=".$dir."images/trv_intersecplus.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

}

}

else

{

if (mysql_num_rows($rs)==$i)

{

echo "<img src=images/trv_end.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

else

{

echo "<img src=images/trv_intersec.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

}
if ($action=="expand"&&openFolder($treeview_cod,$parent["treeview_cod"]))

{

echo "<img src=images/trv_openfolder.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

else

{

echo "<img src=images/trv_closedfolder.gif height=18 width=18 border=0 vspace=0 hspace=0 align=left>";

}

echo "</a>";
//Here you can change where the link of the folder points too.

//You really should to edit it...
echo "<a href=main.php?treeview_cod=".$parent["treeview_cod"]." target=treeview_main>";

echo "&nbsp;&nbsp;".$parent["treeview_name"];

echo "</td>";

echo "</tr>";

if ($action=="expand"&&openFolder($treeview_cod,$parent["treeview_cod"]))

{

buildTreeView($action,$treeview_cod,$parent["treeview_cod"],$depth,$parent_last);

}
$i++;

}

}

}
//Now the old plain and boring HTML stuff...
echo "
<html>

<title>TreeView</title>

<style>
a {

color:#000000;

font-family:'Tahoma,Arial,Helvetica';

font-size: 8pt;

text-decoration:none;

}
a:hover {

color:#0000FF;

font-family:'Tahoma,Arial,Helvetica';

font-size: 8pt;

text-decoration:none;

}
.titulo {

color:#000000;

font-family:'Tahoma,Arial,Helvetica';

font-size: 30pt;

text-decoration:none;

font-weight:bold;

}
p {

color:#000000;

font-family:'Tahoma,Arial,Helvetica';

font-size: 11px;

text-decoration:none;

}
text {

color:#000000;

font-family:'Tahoma,Arial,Helvetica';

font-size: 11px;

text-decoration:none;

}
</style>
<body bgcolor=#FFFFFF>";
//Building the table for display of the treeview
echo "<table border=0 cellpadding=0 cellspacing=0 width=100%>";
echo "<tr>";

echo "<td valign=top>";

echo "<p><img src=images/trv_root.gif>";

echo "&nbsp;&nbsp;<font size=4><b>Root</b></font>";

echo "</td>";

echo "</tr>";
echo "<tr height=3>";

echo "<td valign=top nowrap>";

echo "</td>";

echo "</tr>";
//Calling the function for the treeview.
buildTreeView($action,$treeview_cod);
//Closing everything...
echo "</table>";

echo "</body></html>"
?>