CodeThatTreePhp is a small and fast PHP script for creating DHTML trees dynamically.
To use CodeThatTreePhp you should:
Copy ct_tree_lite.php to your include directory (so it will be visible by other scripts via operator require("ct_tree_lite.php").
Create a table in your database with tree structure.
Usually the table will contain
item action hyperlink.
You may also store any additional information about tree items.
Here you can see an example for such a table creation:
CREATE TABLE tree_nodes (
node_id int(11) NOT NULL default '0',
node_name varchar(30) NOT NULL default '',
node_parent int(11) NOT NULL default '0',
PRIMARY KEY (node_id)
Fill the table with your data. For example:
INSERT INTO tree_nodes VALUES (0, 'Root', -1);
INSERT INTO tree_nodes VALUES (2, '2_2', 0);
INSERT INTO tree_nodes VALUES (3, '3_3', 2);
INSERT INTO tree_nodes VALUES (4, '4_4', 0);
INSERT INTO tree_nodes VALUES (5, '5_5', 4);
INSERT INTO tree_nodes VALUES (6, '6_6', 0);
INSERT INTO tree_nodes VALUES (7, '7_7', 6);
INSERT INTO tree_nodes VALUES (8, '8_8', 0);
INSERT INTO tree_nodes VALUES (9, '9_9', 8);
INSERT INTO tree_nodes VALUES (10, '10_10', 0);
INSERT INTO tree_nodes VALUES (11, '11_11', 10);
INSERT INTO tree_nodes VALUES (12, '12_12', 0);
INSERT INTO tree_nodes VALUES (13, '13_13', 12);
INSERT INTO tree_nodes VALUES (14, '14_14', 0);
INSERT INTO tree_nodes VALUES (15, '15_15', 14);
Create a php page that will display the tree.
The page will use CodeThatTreePhp script
The page should include procedure that will connect to your database and loop on your tree structure.
/* declare some relevant variables */
$hostname = "localhost"; /* This is the hostname on which your MySQL is running */
$dbName = "ct"; /* This is your database name */
$username = "root"; /* This is user name to connect to your database */
$password = ""; /* This is user password */
/* Make connection to database */
mysql_connect($hostname, $username, $password)
OR DIE("Unable to connect to database");
/* Select the database to be processed */
@mysql_select_db( "$dbName") or die( "Unable to select database");
Function ReadLevel($id, $level = 0)
$str = "";
/* Prepare and execute the SQL query statement */
$query = "SELECT node_id, node_parent, node_name " .
"FROM tree_nodes where node_parent=".$id;
$result = mysql_query($query);
/* Loop on results */
while (list($node_id, $node_parent, $node_name) =
$subitems = ReadLevel($node_id, $level + 1);
$str .= CreateItem(
$node_id, // Id of the node, used for identification
$node_name, // Name of the node
// (may be some html code),
// will be displayed
"#", // URL
"", // URL target
$subitems == "" ? "clsItem" : "clsFolder", // CSS
$subitems, // subitems string
$level // level of the item
// for proper offset
Please note this is recursive procedure that calls CreateItem function for tree item creation.
Create the page body. The body should include Preface function call. The function writes some needful scripts to destination page.
Call your recursive ReadLevel function at the place you want the tree to appear.
Sample page body:
<link href="ctc.css" rel="stylesheet" type="text/css">
<title>CodeThatTree lite sample</title>
<body leftmargin="0" topmargin="0" marginwidth="0"
For complete page sample see test.php file.
Add some css decoration to your tree. See ctc.css file for example.
See how this example works here.
That is all! You can enjoy the tree in created page.
CodeThat Tree is aimed to be fast and lightweight therefore it has some limitation.
The tree works fine with NN 6-7, IE 5-6, Opera 7.
NN4 shows the tree, links are working, but nodes cannot be expanded/collapsed.
The tree items exist as single layer and don't support CodeThatTree Pro decoration features such as borders, shadows, advanced over effects. However it supports CSS and still can be decorated by custom fonts, color, etc.
The tree doesn't support creation from xml.