Click to See Complete Forum and Search --> : treeview menu dynamic


gayatri
03-30-2006, 10:58 PM
hi there
i want to build a treeview structure for my menu, like what we find in windows for menu display
something like folder image and folder name, after clicking folder image/name it should display files under that
i want to design it in php with the help of js
i have managed to do it but it's static
i mean i give filename and it displays those
but what i want is,
it should take that folder name, search in mysql database and display those filenames stored in database
any help would be gr8ly appreciated
thanx

LiLcRaZyFuZzY
03-30-2006, 11:14 PM
you have the tree structure in your dB?

gayatri
03-30-2006, 11:51 PM
i have a table tree_parent which contains parent entries
and a table tree_child which has child entries and parent_id

LiLcRaZyFuZzY
03-31-2006, 03:08 AM
ok, loop through the parent table an for each ID loop through the child table and get those with the same parent ID

gayatri
03-31-2006, 05:31 AM
i have managed till that, but what my main problem is how to make entry of a child entry which has it's child entries
i mean
it's something like
parent1
-child1
-child2
-child3
--subchild 31
--subchild 32
-child4
parent2
-child5
-child6

i want to manage for
--subchild 31
--subchild 32
these entries
which are child entries for child 3
how can i store these in database and how to access them back?
thanx a lot for ur help

bokeh
03-31-2006, 08:18 AM
you have the tree structure in your dB?
I don't think you need one. CREATE TABLE `directories` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`parent_id` INT NOT NULL ,
PRIMARY KEY ( `id` )
);Starting with the named directory get its id. Now do a search for all directories with that id as the parent_id. You can do this recursively. Personally I'd just grab the whole table and do the donkey work with PHP but only because I'm not very SQL savvy. I'm sure there is a method with SQL to do all this in one query... or maybe not.

gayatri
04-04-2006, 12:56 AM
hi there!

i have managed with the database table storage for tree structure
my table is like
id parent_id name desc
1 0 parent1
2 1 child11
3 1 child21
5 0 parent2
6 5 child21
7 2 subchild

like that

i am trying for a recursive function now which will take up name and displays the link
my function is



function callbranch($parent)
{
//$parent is name say here parent1

$parent2=mysql_query("SELECT distinct parent_id FROM MenuTree WHERE (parent_id NOT IN (SELECT id FROM MenuTree WHERE parent_id='0') ) AND (parent_id!='0')");
while($parentrow2=mysql_fetch_array($parent2))
$parentname2[]=$parentrow2["parent_id"];

$pid=mysql_result(db_query("select id from MenuTree where name='$parent'"));

$op .='<table border=0 cellpadding="1" cellspacing=1><tr><td width="16"><a id="xproducts" href="javascript:Toggle(\''.$parent.'\');"><img src="image1" width="16" height="16" hspace="0" vspace="0" border="0"></a></td><td><b><a id="xproducts" href="javascript:Toggle(\''.$parent.'\');">'.$parent.'</a></b></table>';
$op .='<div id="'.$parent.'" style="display: none; margin-left: 2em;">';

$child=mysql_query("select name,id from MenuTree where parent_id='$pid'");
while($childrow=mysql_fetch_array($child))
{
if (in_array($childrow["id"], $parentname2))
{
$getop=callbranch($childrow["name"]);
//echo $getop;
$op .=$getop;
}
else
{
$op .='<table border=0 cellpadding="1" cellspacing=1><tr><td width="16"><img src="image1" width="16" height="16" hspace="0" vspace="0" border="0"></td><td>'.$childrow["name"].'</a></td></tr></table>';
}
}
return $op;
}



parent1
-child11
-child21
--subchild
-parent2

problem is parent2 is displayed under parent1
i don't know where to output recursive function's o/p?
like wise here $getop

i would be thankful, if someone take time and point out where the problem is
thanx

bokeh
04-04-2006, 06:20 AM
First a couple of problems... Your directory table does not have a root element from which everything decends. This is not essential but is ideal. Secondly your I think you will run into trouble if you have directories with the same name and the name is relied on as the query root element. You could make the name field of the directories table unique but this could have other implications. Anyway I came up with the following but I only had a couple of minutes to spend on it. A multi dimentional array is created with the queried directory as the root array element. <?php

$username = ''; # ???
$password = ''; # ???
$database = ''; # ???
$tablename = 'MenuTree';
$directory = 'parent1';

$mysql = mysql_connect('localhost', $username, $password);
mysql_select_db($database);

$query = "SELECT * FROM `$tablename` WHERE `name` = '$directory'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0)
{
$branch = branches(mysql_fetch_assoc($result), $tablename);
}
else
{
# no result returned
}

function branches($row, $tablename)
{
$query = "SELECT * FROM `$tablename` WHERE `parent_id` = '{$row['id']}'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$branch[$row['name']] = branches($row, $tablename);
}
return $branch;
}

# test output
echo '<pre>'; print_r($branch); echo '</pre>';

?>This is not necessarily the best way to do this, it is just what I came up with after a minute's thought. The maybe a way to do this with SQL alone but I'll leave the answer to that to one of the SQL experts.

rch10007
04-04-2006, 06:58 AM
i'm not 100% sure - but i think was reading somewhere about making a treeview menu with just CSS.

all of your child links would be on the page, but hidden until you clicked on the parent - then they would open under or to the side of the parent element, or wherever you put them - that is the beauty of CSS.

I would ask about this in the CSS forum to discuss a simpler solution than fancy DB and php tricks.

come to think of it, CSS won't display the menu tree if someone turns off the page style and JS won't help if that is turned off - maybe php and sql is the best way to go to ensure the menu's function.

gayatri
04-04-2006, 07:25 AM
guys thanx for php
i'm not 100% sure - but i think was reading somewhere about making a treeview menu with just CSS.

yes it is possible to design it in CSS or HTML but it provides static data
and i am in need of dynamic, i mean user can input any number of parents n child entries, those will be consequently stored in database and from php code i need to access it

First a couple of problems... Your directory table does not have a root element from which everything decends. This is not essential but is ideal. Secondly your I think you will run into trouble if you have directories with the same name and the name is relied on as the query root element. You could make the name field of the directories table unique but this could have other implications. Anyway I came up with the following but I only had a couple of minutes to spend on it. A multi dimentional array is created with the queried directory as the root array element.

i have root elements with parent_id='0'
what else shud i do, i really can't get u, do i need to create a basic root element which has list of directories like parent1,parent2 etc???
and for same name u mentioned, i will take care for that, user shud not same names again,

bokeh
04-04-2006, 08:00 AM
i have root elements with parent_id='0'ok! I just assumed you didn't because auto incrementing fields start at 1. Did you try my code? Just change the name field to unique and then my code should work without any chance of a collision.

gayatri
04-04-2006, 11:59 PM
yes i have tried ur code, and it's working perfect!
what i want to add is i need to add facility of onclick/toggle display
i mean what we find in windows for directory and files listing
well, above all i have my code ready which reads entries from database and displays as root entries and onclick child entries for that parent
thanx for ur help

bokeh
04-05-2006, 04:18 AM
Ok, I have a method to do that. It will need Javascript and as a back-up for people with javascript disabled the menu will automatically be displayed open. How much time do you have?

bokeh
04-05-2006, 08:38 PM
First here is the working demo! (http://bokehman.com/tree.menu/) I'd be interested to hear people's comments on this, and how to improve it further. The PHP code used to retrieve the data from MySQL and output the HTML is as follows: <?php
# PHP section

# Start: edit these variables
$username = 'root'; # ???
$password = ''; # ???
$database = 'test'; # ???
$tablename = 'directories';
$directory = 'root';
# End: edit these variables

$mysql = mysql_connect('localhost', $username, $password);
mysql_select_db($database);

$query = "SELECT * FROM `$tablename` WHERE `name` = '$directory'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0)
{
$branch[$directory] = branches(mysql_fetch_assoc($result), $tablename);
$branch = BranchToList($branch);
}
else
{
# no result returned
}

function branches($row, $tablename)
{
$query = "SELECT * FROM `$tablename` WHERE `parent_id` = '{$row['id']}'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$branch[$row['name']] = branches($row, $tablename);
}
return $branch;
}

function BranchToList($branch)
{
static $indent;
$indent .= ' ';
$output = "$indent<ul>\n";
$indent .= ' ';
foreach($branch as $k => $v)
{
$array = is_array($v);
$output .= $indent.'<li>'."\n";
$indent .= ' ';
$output .= $indent.'<img src="'.(($array) ? './opened.dir' : './gray.dir' ).
'" alt="'.(($array) ? 'opened' : 'gray' ).
'" '.(($array) ? 'onmouseover="this.style.cursor = \'pointer\'" onclick="toggle(this)" onkeypress="toggle(this)"' : '' ).
'>'."\n";
$output .= $indent.$k."\n";
$indent = substr($indent, 1);
if($array)
{
$output .= BranchToList($v);
}
$output .= "$indent</li>\n";
}
$indent = substr($indent, 1);
$output .= "$indent</ul>\n";
$indent = substr($indent, 1);
return $output;
}

# save bandwidth
ob_start('ob_gzhandler');

# HTML section
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Tree Menu</title>
<style type="text/css">
#Menu{
background-color:#fff;
color: #000;
font-size: 10pt;
font-family: verdana, sans-serif;
}

#Menu ul{
list-style:none;
clear:left;
}

#Menu li{
margin: 5px 0 0 -18px;

clear:left;
}

#Menu ul img{
float:left;
margin: 0 4px 0 0;
}
</style>
</head>
<body>
<div id="Menu">
<?php

echo $branch;

?>
<script type="text/javascript" src="./javascript/"></script>
</div>
</body>
</html>And seeing as this is the PHP forum I'm not going to post the Javascript but you can grab it off the demo.

prathikanoe
01-22-2010, 05:55 AM
hi..thanks .i was looking for same kind of tree..but i need a javascript code..please will u tell me wr i can get that javascript code..i serached in the link but not able to find..

thanks in advance

bokeh
01-22-2010, 03:01 PM
hi..thanks .i was looking for same kind of tree..but i need a javascript code..please will u tell me wr i can get that javascript code..i serached in the link but not able to find..What do you think is controlling the demo?

prathikanoe
01-22-2010, 10:48 PM
i think the js will control the flow as it is a dynamic flow even i need a php recursive function.can you just assist me how can i proceed in the flow.