Home > Archive > PHP DB > March 2004 > RE: [PHP-DB] Tree structure - how to show only current branch ??
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
RE: [PHP-DB] Tree structure - how to show only current branch ??
|
|
| Mark A Galbreath 2004-03-30, 9:33 am |
| Already been done:
http://www.destroydrop.com/javascripts/tree/
Mark
-----Original Message-----
From: -{ Rene Brehmer }- [mailto:metalbunny@metalbunny.net]
Sent: Tuesday, March 30, 2004 7:50 AM
To: php-db@lists.php.net
Subject: [PHP-DB] Tree structure - how to show only current branch ??
Ok, Tom Reed got my thinker running big time ... and I've been trying to=20
build an expandable and modifiable tree structure where it only displays=20
the branch leading to the current folder... Showing the entire tree is=20
easy, but how do I change the code to only display the branch we need???=20
.... 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=20
being fixed in the DB. What I want to create is the possibility to mode=20
folders around within the tree ... literally moving branches from one pla=
ce=20
to another, while retaining their content.
DB structure is simply this (I want to get rid of the level field if it's=
=20
at all possible to count the levels with recursive functions, but for now=
=20
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...rver=3D1&db=3D=
tree+tes
t&table=3Dfolders&pos=3D0&session_max_rows=3D30&disp_direction=3Dhorizont=
al&repeat_c
ells=3D100&dontlimitchars=3D0& sql_query=3DSELECT++%2A+%0AFROM++%60fold
ers=
%60++ORDE
R+BY+%60folderID%60+ASC>folderID=20
<http://localhost/phpmyadmin/sql.php...rver=3D1&db=3D=
tree+tes
t&table=3Dfolders&pos=3D0&session_max_rows=3D30&disp_direction=3Dhorizont=
al&repeat_c
ells=3D100&dontlimitchars=3D0& sql_query=3DSELECT++%2A+%0AFROM++%60fold
ers=
%60++ORDE
R+BY+%60parentID%60+ASC>parentID=20
<http://localhost/phpmyadmin/sql.php...rver=3D1&db=3D=
tree+tes
t&table=3Dfolders&pos=3D0&session_max_rows=3D30&disp_direction=3Dhorizont=
al&repeat_c
ells=3D100&dontlimitchars=3D0& sql_query=3DSELECT++%2A+%0AFROM++%60fold
ers=
%60++ORDE
R+BY+%60level%60+ASC>level=20
<http://localhost/phpmyadmin/sql.php...rver=3D1&db=3D=
tree+tes
t&table=3Dfolders&pos=3D0&session_max_rows=3D30&disp_direction=3Dhorizont=
al&repeat_c
ells=3D100&dontlimitchars=3D0& sql_query=3DSELECT++%2A+%0AFROM++%60fold
ers=
%60++ORDE
R+BY+%60name%60+ASC>name=20
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=
=20
main function below prints the root structure...:
function count_children($parentID) {
// count number of children in folder
$count =3D mysql_query("SELECT COUNT(*) AS num_children FROM folders W=
HERE=20
`parentID`=3D'$parentID'");
$numrows =3D mysql_fetch_array($count);
return $numrows['num_children'];
}
function print_children($parentID) {
// print the branch of sub-folders
$children =3D mysql_query("SELECT folderID,level,name FROM folders WHE=
RE=20
`parentID`=3D'$parentID'");
while($child =3D mysql_fetch_array($children)) {
$folderID =3D $child['folderID'];
$name =3D $child['name'];
$level =3D $child['level'];
for ($i =3D 0; $i < $level; $i++) {
echo('·');
}
echo("· <a
href=3D\"test1.php?folderID=3D$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 =3D mysql_query("SELECT folderID,name FROM folders WHERE=20
`parentID`=3D'0'");
while($folder =3D mysql_fetch_array($parents)) {
$folderID =3D $folder['folderID'];
$name =3D $folder['name'];
echo("· <a
href=3D\"test1.php?folderID=3D$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:
=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D1>paren=
t 1
=B7=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D6>ch=
ild of 1
=B7=B7=B7 <http://localhost/tests/tree%20struc...p?folderID=3D9=
>sub-child
1
=B7=B7=B7 <http://localhost/tests/tree%20struc...p?folderID=3D1=
0>sub-child
2
=B7=B7=B7=B7=B7 <http://localhost/tests/tree%20struc...st1.php?folder=
ID=3D11>sub-sub
1
=B7=B7=B7=B7=B7=B7=20
<http://localhost/tests/tree%20struc...p?folderID=3D13>sub-sub-=
sub 1
=B7=B7=B7=B7=B7 <http://localhost/tests/tree%20struc...st1.php?folder=
ID=3D12>sub-sub
2
=B7=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D8>ch=
ild 2 of
1
=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D2>paren=
t 2
=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D3>paren=
t 3
=B7=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D7>ch=
ild of 3
=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D4>paren=
t 4
=B7 <http://localhost/tests/tree%20struc...hp?folderID=3D5>paren=
t 5
What I wanna do is make it possible to click on, say, 'sub-child 2', and=20
have it a. expand the entire branch under 'parent 1' leading to 'sub-chil=
d=20
2', but not expand any other branches in the root structure, AND, b. have=
=20
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=
=20
all that difficult to handle (or what ?)...
Last tree structure I did was mostly logical, not visual ... and it was=20
done in VB long ago ... so it's not code that can be recycled...
TIA
Rene
--=20
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...
--=20
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
| |
| Paul Miller 2004-03-30, 10:34 am |
| Ya it has - that is a great script!
-----Original Message-----
From: Galbreath, Mark A [mailto:GalbreathMA@state.gov]
Sent: Tuesday, March 30, 2004 7:15 AM
To: '-{ Rene Brehmer }-'; 'php-db@lists.php.net'
Subject: RE: [PHP-DB] Tree structure - how to show only current branch
??
Already been done:
http://www.destroydrop.com/javascripts/tree/
Mark
-----Original Message-----
From: -{ Rene Brehmer }- [mailto:metalbunny@metalbunny.net]
Sent: Tuesday, March 30, 2004 7:50 AM
To: php-db@lists.php.net
Subject: [PHP-DB] 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...erver=1&db=tree
+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
at_c
ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%60++
ORDE
R+BY+%60folderID%60+ASC>folderID
<http://localhost/phpmyadmin/sql.php...erver=1&db=tree
+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
at_c
ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%60++
ORDE
R+BY+%60parentID%60+ASC>parentID
<http://localhost/phpmyadmin/sql.php...erver=1&db=tree
+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
at_c
ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%60++
ORDE
R+BY+%60level%60+ASC>level
<http://localhost/phpmyadmin/sql.php...erver=1&db=tree
+tes
t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
at_c
ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%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('·');
}
echo("· <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("· <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%20struc....php?folderID=1>parent 1
... <http://localhost/tests/tree%20struc....php?folderID=6>child
of 1 ...
<http://localhost/tests/tree%20struc....php?folderID=9>sub-child
1
....
<http://localhost/tests/tree%20struc...php?folderID=10>sub-child
2
......
<http://localhost/tests/tree%20struc...php?folderID=11>sub-sub
1
.......
<http://localhost/tests/tree%20struc...php?folderID=13>sub-sub-s
ub 1 .....
<http://localhost/tests/tree%20struc...php?folderID=12>sub-sub
2
... <http://localhost/tests/tree%20struc....php?folderID=8>child 2
of 1 .
<http://localhost/tests/tree%20struc....php?folderID=2>parent 2 .
<http://localhost/tests/tree%20struc....php?folderID=3>parent 3
... <http://localhost/tests/tree%20struc....php?folderID=7>child
of 3 .
<http://localhost/tests/tree%20struc....php?folderID=4>parent 4 .
<http://localhost/tests/tree%20struc....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
| |
| Ignatius Reilly 2004-03-30, 10:35 am |
| The same can be achieved by using the excellent PEAR HTML_TreeMenu class.
Key advantage, of course, is that the API is in PHP.
HTH
Ignatius
_________________________
----- Original Message -----
From: "Paul Miller" <pmiller@system-wise.com>
To: <php-db@lists.php.net>
Sent: Tuesday, March 30, 2004 4:22 PM
Subject: RE: [PHP-DB] Tree structure - how to show only current branch ??
> Ya it has - that is a great script!
>
> -----Original Message-----
> From: Galbreath, Mark A [mailto:GalbreathMA@state.gov]
> Sent: Tuesday, March 30, 2004 7:15 AM
> To: '-{ Rene Brehmer }-'; 'php-db@lists.php.net'
> Subject: RE: [PHP-DB] Tree structure - how to show only current branch
> ??
>
>
> Already been done:
>
> http://www.destroydrop.com/javascripts/tree/
>
> Mark
>
> -----Original Message-----
> From: -{ Rene Brehmer }- [mailto:metalbunny@metalbunny.net]
> Sent: Tuesday, March 30, 2004 7:50 AM
> To: php-db@lists.php.net
> Subject: [PHP-DB] 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...erver=1&db=tree
> +tes
> t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
> at_c
> ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%60++
> ORDE
> R+BY+%60folderID%60+ASC>folderID
> <http://localhost/phpmyadmin/sql.php...erver=1&db=tree
> +tes
> t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
> at_c
> ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%60++
> ORDE
> R+BY+%60parentID%60+ASC>parentID
> <http://localhost/phpmyadmin/sql.php...erver=1&db=tree
> +tes
> t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
> at_c
> ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%60++
> ORDE
> R+BY+%60level%60+ASC>level
> <http://localhost/phpmyadmin/sql.php...erver=1&db=tree
> +tes
> t&table=folders&pos=0&session_max_rows=30&disp_direction=horizontal&repe
> at_c
> ells=100&dontlimitchars=0& sql_query=SELECT++%2A+%0AFROM++%60folder
s%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('·');
> }
> echo("· <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("· <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%20struc....php?folderID=1>parent 1
> .. <http://localhost/tests/tree%20struc....php?folderID=6>child
> of 1 ...
> <http://localhost/tests/tree%20struc....php?folderID=9>sub-child
> 1
> ...
> <http://localhost/tests/tree%20struc...php?folderID=10>sub-child
> 2
> .....
> <http://localhost/tests/tree%20struc...php?folderID=11>sub-sub
> 1
> ......
> <http://localhost/tests/tree%20struc...php?folderID=13>sub-sub-s
> ub 1 .....
> <http://localhost/tests/tree%20struc...php?folderID=12>sub-sub
> 2
> .. <http://localhost/tests/tree%20struc....php?folderID=8>child 2
> of 1 .
> <http://localhost/tests/tree%20struc....php?folderID=2>parent 2 .
> <http://localhost/tests/tree%20struc....php?folderID=3>parent 3
> .. <http://localhost/tests/tree%20struc....php?folderID=7>child
> of 3 .
> <http://localhost/tests/tree%20struc....php?folderID=4>parent 4 .
> <http://localhost/tests/tree%20struc....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
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
|
|
|
|
|