PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,439 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,491 people online right now. Registration is fast and FREE... Join Now!




tree structure using php and js

 

tree structure using php and js

gotstuck

29 Mar, 2006 - 05:20 AM
Post #1

New D.I.C Head
*

Joined: 22 Mar, 2006
Posts: 14



Thanked: 1 times
My Contributions
hi guys
i need to design a tree structure which we find in windows for directory listing etc

or just chk the link
http://www.softcomplex.com/products/tigra_tree_menu/

here in sample section
i want to design something like this
can anyone give some guidelines
i am totally new to js and i am good at php
i have started with js a few days back
any help would be gr8ly appreciated
thanx in advance

User is offlineProfile CardPM
+Quote Post


snoj

RE: Tree Structure Using Php And Js

29 Mar, 2006 - 12:03 PM
Post #2

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,369



Thanked: 29 times
Dream Kudos: 775
My Contributions
Why not just use the script you pointed to?

Or were you wanting to make your own for learning purposes?
User is offlineProfile CardPM
+Quote Post

gotstuck

RE: Tree Structure Using Php And Js

29 Mar, 2006 - 08:49 PM
Post #3

New D.I.C Head
*

Joined: 22 Mar, 2006
Posts: 14



Thanked: 1 times
My Contributions
yes want to make mine
and that to with some php functions
i have the script with html and js
but i want to make the contents dynamic
i mean it can be taken from database for main and sub menus
any suggessions??
User is offlineProfile CardPM
+Quote Post

snoj

RE: Tree Structure Using Php And Js

29 Mar, 2006 - 10:19 PM
Post #4

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,369



Thanked: 29 times
Dream Kudos: 775
My Contributions
Just plan out how you'll store parent-child relationships in a database.
User is offlineProfile CardPM
+Quote Post

gotstuck

RE: Tree Structure Using Php And Js

31 Mar, 2006 - 04:30 AM
Post #5

New D.I.C Head
*

Joined: 22 Mar, 2006
Posts: 14



Thanked: 1 times
My Contributions
yes that's truly critical to decide
i have reached till, parent entries display and onclick parent child entry display
but struggling with a parent entry within parent
yes, and confused how to store such entries in database
i have one parent table and the other child table which carries chald name and parent_id for that child
but what shud i do for those links which exists under parent but have their child entries too
i have structure 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?

i am confused, plz guide me

This post has been edited by gotstuck: 31 Mar, 2006 - 04:42 AM
User is offlineProfile CardPM
+Quote Post

snoj

RE: Tree Structure Using Php And Js

31 Mar, 2006 - 09:14 AM
Post #6

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,369



Thanked: 29 times
Dream Kudos: 775
My Contributions
Easy, use id's as parent id's with 0 or some unique ID that represents root.

Say we have three entries in the database.

CODE
id,parent,name,other_properties
0,0,root,foo
1,0,a_parent,but_a_child_of_root
2,1,child,child_of_a_parent
3,0,another_child,child_of_root
ect.,ect.,ect.,ect.

User is offlineProfile CardPM
+Quote Post

gayatri

RE: Tree Structure Using Php And Js

3 Apr, 2006 - 09:36 PM
Post #7

D.I.C Head
Group Icon

Joined: 13 Feb, 2006
Posts: 62



Thanked: 2 times
Dream Kudos: 25
My Contributions
hi there!
i am posting here just bcuz i had came across same probs
i have managed with the database table storage for tree structure

id parent_id name status tstamp
1 0 Parent_1 1 0
2 1 child_11 1 0
3 1 child_12 1 0
4 1 child_13 1 0
5 1 child_14 1 0
6 4 child_13_1 1 0
7 4 child_13_2 1 0
8 0 Parent_2 1 0
9 8 child_21 1 0
10 8 child_22 1 0

like that

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

CODE

<?php

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

 $childparents=mysql_query("select distinct parent_id from MenuTree");
 while($childparents_row=mysql_fetch_array($childparents))
    $childp[]=$childparents_row["parent_id"];

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

$op .='<script type="text/javascript">
function Toggle(item)
{
  obj=document.getElementById(item);
  visible=(obj.style.display!="none")

  key=document.getElementById("x" + item);
  if (visible)
  {
    obj.style.display="none";
    key.innerHTML="<img src=\'imageclose.jpg\' width=\'16\' height=\'16\' hspace=\'0\' vspace=\'0\' border=\'0\'>";
  }
  else
  {
     obj.style.display="block";
     key.innerHTML="<img src=\'imageopen.jpg\' width=\'16\' height=\'16\' hspace=\'0\' vspace=\'0\' border=\'0\'>";
  }
}
</script>';

$op .='<table border=0 cellpadding="10" cellspacing=0><tr><td>';
$parent1=mysql_query("select id,parent_id,name from MenuTree where parent_id='0'");
while($parentrow=mysql_fetch_array($parent1))
{
 $parentname[]=$parentrow["name"];
 $parentid[]=$parentrow["id"];
}
for ($i=0;$i<count($parentname);$i++)
{
 $getop =callbranch($parentname[$i]);
 $op .=$getop;
 $op .='</div>';
}
$op .='</td></tr></table>';
echo $op;
?>


well, i have fixed it
chk out for the code i have modified it

This post has been edited by gayatri: 4 Apr, 2006 - 05:00 AM
User is offlineProfile CardPM
+Quote Post

snoj

RE: Tree Structure Using Php And Js

3 Apr, 2006 - 11:03 PM
Post #8

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,369



Thanked: 29 times
Dream Kudos: 775
My Contributions
I'll try to get back to you in the morning after I've thought it though some more. But is there a place where this is located so I can see the output?
User is offlineProfile CardPM
+Quote Post

gayatri

RE: Tree Structure Using Php And Js

3 Apr, 2006 - 11:06 PM
Post #9

D.I.C Head
Group Icon

Joined: 13 Feb, 2006
Posts: 62



Thanked: 2 times
Dream Kudos: 25
My Contributions
not actually
but i will edit my earlier post and post the whole code
so that it can be run at a time in whole, by just copy n paste
gimme some time for that
thanx
User is offlineProfile CardPM
+Quote Post

gayatri

RE: Tree Structure Using Php And Js

4 Apr, 2006 - 05:04 AM
Post #10

D.I.C Head
Group Icon

Joined: 13 Feb, 2006
Posts: 62



Thanked: 2 times
Dream Kudos: 25
My Contributions
i have fixed it.
i need to submit it by dayend, so i was behind it madly
and now i have the output
chk out, i have modified my earlier post, and it's same as what i have now(my code)
thanx for ur help
if u have time, just go through code and let me know, in case u find anything that need to be done more on this
i am greedy for good code
so i hope i will get some tips here, lke i got earlier
thanx again

This post has been edited by gayatri: 4 Apr, 2006 - 05:10 AM
User is offlineProfile CardPM
+Quote Post

Jono

RE: Tree Structure Using Php And Js

26 Apr, 2006 - 02:19 PM
Post #11

New D.I.C Head
*

Joined: 26 Apr, 2006
Posts: 2



Thanked: 1 times
My Contributions
You said earlier that you were using two tables to hold the child and parent relationships in your table????

There is much better technique, developed by Joe Celko, which is called nested sets and uses only one table for any number of child relationships. Only a single line SQL query is needed to retieve any of the tree relationships or statistics. A full explanation can be found at MySQL, or in Joe Celko's Book, Trees and Hierarchies in SQL for Smarties.
User is offlineProfile CardPM
+Quote Post

snoj

RE: Tree Structure Using Php And Js

26 Apr, 2006 - 02:40 PM
Post #12

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,369



Thanked: 29 times
Dream Kudos: 775
My Contributions
That's what he's doing.
User is offlineProfile CardPM
+Quote Post

Jono

RE: Tree Structure Using Php And Js

26 Apr, 2006 - 03:42 PM
Post #13

New D.I.C Head
*

Joined: 26 Apr, 2006
Posts: 2



Thanked: 1 times
My Contributions
My apologies, it just sounded like he was using two tables, with a static hierarchy.
User is offlineProfile CardPM
+Quote Post

cyberscribe

RE: Tree Structure Using Php And Js

8 May, 2006 - 10:01 PM
Post #14

humble.genius
Group Icon

Joined: 5 May, 2002
Posts: 1,062



Thanked: 9 times
Dream Kudos: 154
My Contributions
Nice to see Mr. Snoj still helping people with PHP.
User is offlineProfile CardPM
+Quote Post

dream2rule

RE: Tree Structure Using Php And Js

27 Jun, 2007 - 01:44 AM
Post #15

New D.I.C Head
*

Joined: 26 Jun, 2007
Posts: 5


My Contributions
QUOTE(gayatri @ 3 Apr, 2006 - 10:36 PM) *

hi there!
i am posting here just bcuz i had came across same probs
i have managed with the database table storage for tree structure

id parent_id name status tstamp
1 0 Parent_1 1 0
2 1 child_11 1 0
3 1 child_12 1 0
4 1 child_13 1 0
5 1 child_14 1 0
6 4 child_13_1 1 0
7 4 child_13_2 1 0
8 0 Parent_2 1 0
9 8 child_21 1 0
10 8 child_22 1 0

like that

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

CODE

<?php

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

  $childparents=mysql_query("select distinct parent_id from MenuTree");
  while($childparents_row=mysql_fetch_array($childparents))
     $childp[]=$childparents_row["parent_id"];

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

$op .='<script type="text/javascript">
function Toggle(item)
{
   obj=document.getElementById(item);
   visible=(obj.style.display!="none")

   key=document.getElementById("x" + item);
   if (visible)
   {
     obj.style.display="none";
     key.innerHTML="<img src=\'imageclose.jpg\' width=\'16\' height=\'16\' hspace=\'0\' vspace=\'0\' border=\'0\'>";
   }
   else
   {
      obj.style.display="block";
      key.innerHTML="<img src=\'imageopen.jpg\' width=\'16\' height=\'16\' hspace=\'0\' vspace=\'0\' border=\'0\'>";
   }
}
</script>';

$op .='<table border=0 cellpadding="10" cellspacing=0><tr><td>';
$parent1=mysql_query("select id,parent_id,name from MenuTree where parent_id='0'");
while($parentrow=mysql_fetch_array($parent1))
{
  $parentname[]=$parentrow["name"];
  $parentid[]=$parentrow["id"];
}
for ($i=0;$i<count($parentname);$i++)
{
  $getop =callbranch($parentname[$i]);
  $op .=$getop;
  $op .='</div>';
}
$op .='</td></tr></table>';
echo $op;
?>


well, i have fixed it
chk out for the code i have modified it


Hey Gayatri, Can you please help me out in determining the parents and childs in the tree structure. How to know about these and how to insert these values into the database.

It would be really helpful

Thanks and Regards,
Dream2rule

User is offlineProfile CardPM
+Quote Post

enumula

RE: Tree Structure Using Php And Js

1 May, 2008 - 10:21 PM
Post #16

New D.I.C Head
*

Joined: 1 May, 2008
Posts: 1

QUOTE(dream2rule @ 27 Jun, 2007 - 02:44 AM) *

QUOTE(gayatri @ 3 Apr, 2006 - 10:36 PM) *

hi there!
i am posting here just bcuz i had came across same probs
i have managed with the database table storage for tree structure

id parent_id name status tstamp
1 0 Parent_1 1 0
2 1 child_11 1 0
3 1 child_12 1 0
4 1 child_13 1 0
5 1 child_14 1 0
6 4 child_13_1 1 0
7 4 child_13_2 1 0
8 0 Parent_2 1 0
9 8 child_21 1 0
10 8 child_22 1 0

like that

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

CODE

<?php

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

  $childparents=mysql_query("select distinct parent_id from MenuTree");
  while($childparents_row=mysql_fetch_array($childparents))
     $childp[]=$childparents_row["parent_id"];

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

$op .='<script type="text/javascript">
function Toggle(item)
{
   obj=document.getElementById(item);
   visible=(obj.style.display!="none")

   key=document.getElementById("x" + item);
   if (visible)
   {
     obj.style.display="none";
     key.innerHTML="<img src=\'imageclose.jpg\' width=\'16\' height=\'16\' hspace=\'0\' vspace=\'0\' border=\'0\'>";
   }
   else
   {
      obj.style.display="block";
      key.innerHTML="<img src=\'imageopen.jpg\' width=\'16\' height=\'16\' hspace=\'0\' vspace=\'0\' border=\'0\'>";
   }
}
</script>';

$op .='<table border=0 cellpadding="10" cellspacing=0><tr><td>';
$parent1=mysql_query("select id,parent_id,name from MenuTree where parent_id='0'");
while($parentrow=mysql_fetch_array($parent1))
{
  $parentname[]=$parentrow["name"];
  $parentid[]=$parentrow["id"];
}
for ($i=0;$i<count($parentname);$i++)
{
  $getop =callbranch($parentname[$i]);
  $op .=$getop;
  $op .='</div>';
}
$op .='</td></tr></table>';
echo $op;
?>


well, i have fixed it
chk out for the code i have modified it


Hey Gayatri, Can you please help me out in determining the parents and childs in the tree structure. How to know about these and how to insert these values into the database.

It would be really helpful

Thanks and Regards,
Dream2rule



Hi

THis is enumula...

can you please send me total code for tree stryture using php...

THanku,
enumula

User is offlineProfile CardPM
+Quote Post

no2pencil

RE: Tree Structure Using Php And Js

1 May, 2008 - 10:26 PM
Post #17

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,232



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
QUOTE(enumula @ 2 May, 2008 - 02:21 AM) *

Hi

THis is enumula...

can you please send me total code for tree stryture using php...


Considering this topic is almost 2 years old, I don't know if anyone is going to reply.

However, we do have the following snippet available
http://www.dreamincode.net/code/snippet1825.htm
User is online!Profile CardPM
+Quote Post

brijesh_mca

RE: Tree Structure Using Php And Js

3 Jul, 2009 - 11:58 PM
Post #18

New D.I.C Head
*

Joined: 3 Jul, 2009
Posts: 1

Can any body please help me to do indexing of parent-child Hierarchy like
1
2
3
3.1
3.1.1
3.1.1.1
3.1.1.1.1
3.2
3.3
4
5
6
6.1
6.2
6.2.1
6.2.1.1
etc.etc.tec...........
etc.etc.tec............


Thanks.

This post has been edited by brijesh_mca: 4 Jul, 2009 - 12:14 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:07AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month