tree structure using php and js

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 27574 Views - Last Post: 04 July 2009 - 12:58 AM Rate Topic: -----

#1 gotstuck  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-March 06

tree structure using php and js

Posted 29 March 2006 - 06:20 AM

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.softcompl...igra_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

Is This A Good Question/Topic? 0
  • +

Replies To: tree structure using php and js

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: tree structure using php and js

Posted 29 March 2006 - 01:03 PM

Why not just use the script you pointed to?

Or were you wanting to make your own for learning purposes?
Was This Post Helpful? 0
  • +
  • -

#3 gotstuck  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-March 06

Re: tree structure using php and js

Posted 29 March 2006 - 09:49 PM

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??
Was This Post Helpful? 0
  • +
  • -

#4 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: tree structure using php and js

Posted 29 March 2006 - 11:19 PM

Just plan out how you'll store parent-child relationships in a database.
Was This Post Helpful? 0
  • +
  • -

#5 gotstuck  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-March 06

Re: tree structure using php and js

Posted 31 March 2006 - 05:30 AM

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 March 2006 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#6 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: tree structure using php and js

Posted 31 March 2006 - 10:14 AM

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.

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.


Was This Post Helpful? 0
  • +
  • -

#7 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: tree structure using php and js

Posted 03 April 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

<?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: 04 April 2006 - 06:00 AM

Was This Post Helpful? 0
  • +
  • -

#8 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: tree structure using php and js

Posted 04 April 2006 - 12:03 AM

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?
Was This Post Helpful? 0
  • +
  • -

#9 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: tree structure using php and js

Posted 04 April 2006 - 12:06 AM

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
Was This Post Helpful? 0
  • +
  • -

#10 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: tree structure using php and js

Posted 04 April 2006 - 06:04 AM

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: 04 April 2006 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

#11 Jono  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 26-April 06

Re: tree structure using php and js

Posted 26 April 2006 - 03:19 PM

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.
Was This Post Helpful? 1

#12 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: tree structure using php and js

Posted 26 April 2006 - 03:40 PM

That's what he's doing.
Was This Post Helpful? 0
  • +
  • -

#13 Jono  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 26-April 06

Re: tree structure using php and js

Posted 26 April 2006 - 04:42 PM

My apologies, it just sounded like he was using two tables, with a static hierarchy.
Was This Post Helpful? 0
  • +
  • -

#14 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: tree structure using php and js

Posted 08 May 2006 - 11:01 PM

Nice to see Mr. Snoj still helping people with PHP.
Was This Post Helpful? 0
  • +
  • -

#15 dream2rule  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 27-June 07

Re: tree structure using php and js

Posted 27 June 2007 - 02:44 AM

View Postgayatri, on 3 Apr, 2006 - 10:36 PM, said:

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

<?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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2