RE: Tree structure - how to show only current branch ??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Already been done:

http://www.destroydrop.com/javascripts/tree/

Mark

-----Original Message-----
From: -{ Rene Brehmer }- [mailto:metalbunny@xxxxxxxxxxxxxx]
Sent: Tuesday, March 30, 2004 7:50 AM
To: php-db@xxxxxxxxxxxxx
Subject:  Tree structure - how to show only current branch ??


Ok, Tom Reed got my thinker running big time ... and I've been trying to 
build an expandable and modifiable tree structure where it only displays 
the branch leading to the current folder... Showing the entire tree is 
easy, but how do I change the code to only display the branch we need??? 
... I've never coded visual trees before, so this is new for me ...

Found several samples with google where the code relies on the structure 
being fixed in the DB. What I want to create is the possibility to mode 
folders around within the tree ... literally moving branches from one place 
to another, while retaining their content.

DB structure is simply this (I want to get rid of the level field if it's 
at all possible to count the levels with recursive functions, but for now 
it stays).

  folderID int(10) UNSIGNED auto-increment
  parentID  int(10) UNSIGNED
  level  tinyint(3) UNSIGNED
  name  varchar(255)

(I'm fully aware that int may be overkill, but this is for testing
purposes...)

test data:
<http://localhost/phpmyadmin/sql.php?lang=en-iso-8859-1&server=1&db=tree+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_c
ells=100&dontlimitchars=0&sql_query=SELECT++%2A+%0AFROM++%60folders%60++ORDE
R+BY+%60folderID%60+ASC>folderID 
<http://localhost/phpmyadmin/sql.php?lang=en-iso-8859-1&server=1&db=tree+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_c
ells=100&dontlimitchars=0&sql_query=SELECT++%2A+%0AFROM++%60folders%60++ORDE
R+BY+%60parentID%60+ASC>parentID 
<http://localhost/phpmyadmin/sql.php?lang=en-iso-8859-1&server=1&db=tree+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_c
ells=100&dontlimitchars=0&sql_query=SELECT++%2A+%0AFROM++%60folders%60++ORDE
R+BY+%60level%60+ASC>level 
<http://localhost/phpmyadmin/sql.php?lang=en-iso-8859-1&server=1&db=tree+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_c
ells=100&dontlimitchars=0&sql_query=SELECT++%2A+%0AFROM++%60folders%60++ORDE
R+BY+%60name%60+ASC>name 

1 0 0 parent 1
2 0 0 parent 2
3 0 0 parent 3
4 0 0 parent 4
5 0 0 parent 5
6 1 1 child of 1
7 3 1 child of 3
8 1 1 child 2 of 1
9 6 2 sub-child 1
10 6 2 sub-child 2
11 10 4 sub-sub 1
12 10 4 sub-sub 2
13 11 5 sub-sub-sub 1

Current code looks like this, the 2 subfunctions prints the branches, the 
main function below prints the root structure...:

function count_children($parentID) {
// count number of children in folder
   $count = mysql_query("SELECT COUNT(*) AS num_children FROM folders WHERE 
`parentID`='$parentID'");
   $numrows = mysql_fetch_array($count);

   return $numrows['num_children'];
}

function print_children($parentID) {
// print the branch of sub-folders
   $children = mysql_query("SELECT folderID,level,name FROM folders WHERE 
`parentID`='$parentID'");

   while($child = mysql_fetch_array($children)) {
     $folderID = $child['folderID'];
     $name = $child['name'];
     $level = $child['level'];

     for ($i = 0; $i < $level; $i++) {
       echo('&middot');
     }
     echo("&middot; <a
href=\"test1.php?folderID=$folderID\">$name</a><br>\n");

     // let's find children... recursive call !!
     if (count_children($folderID) > 0) {
       print_children($folderID);
     }
   }
}

// get root parents -- main tree function
$parents = mysql_query("SELECT folderID,name FROM folders WHERE 
`parentID`='0'");

while($folder = mysql_fetch_array($parents)) {
   $folderID = $folder['folderID'];
   $name = $folder['name'];

   echo("&middot; <a
href=\"test1.php?folderID=$folderID\">$name</a><br>\n");

   // let's find children...
   if (count_children($folderID) > 0) {
     print_children($folderID);

   }
}


The output of all this looks like this:

· <http://localhost/tests/tree%20structure/test1.php?folderID=1>parent 1
·· <http://localhost/tests/tree%20structure/test1.php?folderID=6>child of 1
··· <http://localhost/tests/tree%20structure/test1.php?folderID=9>sub-child
1
··· <http://localhost/tests/tree%20structure/test1.php?folderID=10>sub-child
2
····· <http://localhost/tests/tree%20structure/test1.php?folderID=11>sub-sub
1
······ 
<http://localhost/tests/tree%20structure/test1.php?folderID=13>sub-sub-sub 1
····· <http://localhost/tests/tree%20structure/test1.php?folderID=12>sub-sub
2
·· <http://localhost/tests/tree%20structure/test1.php?folderID=8>child 2 of
1
· <http://localhost/tests/tree%20structure/test1.php?folderID=2>parent 2
· <http://localhost/tests/tree%20structure/test1.php?folderID=3>parent 3
·· <http://localhost/tests/tree%20structure/test1.php?folderID=7>child of 3
· <http://localhost/tests/tree%20structure/test1.php?folderID=4>parent 4
· <http://localhost/tests/tree%20structure/test1.php?folderID=5>parent 5

What I wanna do is make it possible to click on, say, 'sub-child 2', and 
have it a. expand the entire branch under 'parent 1' leading to 'sub-child 
2', but not expand any other branches in the root structure, AND, b. have 
it not expand beyond showing the sub-folders of 'sub-child 2'

One thing at a time, of course, figure out a. first, then b. shouldn't be 
all that difficult to handle (or what ?)...

Last tree structure I did was mostly logical, not visual ... and it was 
done in VB long ago ... so it's not code that can be recycled...


TIA

Rene
-- 
Rene Brehmer
aka Metalbunny

~ If you don't like what I have to say ... don't read it ~

http://metalbunny.net/
References, tools, and other useful stuff...

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux