WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : 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.
Categories : PHP, HTML and PHP, Navigation Click here to Update Your Picture
Pablo Sanchez
Date : Jul 30th 2002
Grade : 2 of 5 (graded 9 times)
Viewed : 17168
File : treeview.zip
Images : No Images for this code example.
Search : More code by Pablo Sanchez
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?

/*

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;
$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))
{

$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)
{

$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"].">";

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 " ".$parent["treeview_name"];
echo "</td>";
echo "</tr>";
if ($action=="expand"&&openFolder($treeview_cod,$parent
["treeview_cod"]))
{
buildTreeView($action,$treeview_cod,$parent
["treeview_cod"]);
}

$i++;
}
}
}

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 " <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>"

?>



A simple class with some HTML output functions that would come in handy for consistent page layout etc.
Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation
Record Set Paging with PHP (RSP)
Categories : PHP, MySQL, Navigation, Databases, HTML and PHP
Navigation by clicking buttons
Categories : PHP, HTML and PHP, Navigation
GonxTabs : Create elegant HTML tabs based interface
Categories : Navigation, HTML, HTML and PHP, PHP
Database resultset navigation
Categories : PHP, HTML and PHP, Databases, MySQL, Navigation
PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use!
Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP
a function that builds an HTML select list from any mysql table.
Categories : PHP, MySQL, HTML and PHP
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
Constantly refresh your PHP/HTML page data.
Categories : PHP, HTML and PHP, Sybase
Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET
Categories : Algorithms, HTML and PHP, PHP, Variables
background music script for random notes in a frame
Categories : PHP, Content Management, HTML and PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Random Image Display
Categories : PHP, Filesystem, Graphics, HTML and PHP
PhpView 0.1 - simple php viewer, using temporary files and frames.
Categories : PHP, PHP Options and Info, Debugging, HTML and PHP
 Kai Zinkernagel wrote : 1259
Hi,

this is another interesting implementation of a PHP based tree view.

http://it.galantus.com/php-treeview-en.php

Regards

Kai
 
 arasu bala wrote :1667
I have tried using ur program , it doesnt work, when u click on the plus sign , it should expand , but it is not so...