Tokyoahead

News
News
Architecture
Architecture
Asian Cooking
Asian Cooking
Finance
Finance
Galapagos
Galapagos
Technology
Technology
Trips & Sights
Trips & Sights
  

How to make a tree-style menu structure

In order to make a tree-style structure that does not have (almost) any limits to width and length, you need to create an index that allocates a certain hierarchy:

LEVEL - ITEM
001 - Folder 1
002 - Folder 2
003 - Folder 3
...
235 - Folder 235

And for the "subfolders"

001001 Subfolder 1 of Folder 1
001002 etc
...

Assuming that you will write those entries into a table, and want to open one specific item in the tree, you will need to display certain other folders also in order to get a correct layout.

For calling entry 001002, the SQL-Structure for this would be (PHP example):

($strlen is the stringlength of "001002", 6)

$sql="SELECT * FROM yourtable WHERE ((SUBSTRING(level,1,$strlen)='001002') AND (LENGTH(level)=$strlen+3)) ";

for ($l=0;$l<($strlen/3);$l++) {
$sql.=" OR ((LENGTH(level)=".(($l+1)*3).") AND (SUBSTRING('001002',1,".($l*3).") = SUBSTRING(level,1,".($l*3).")))";
}
$sql.=" ORDER BY level;";

(".=" concatenates strings in PHP, please find your appropriate syntax.)

The first WHERE ((...) AND (...)) gets all subfolders of the level. Then you take each 3-digit-portion of the level and open all its first-level parallel folders. This causes that you can see all direct subfolders of level 001 always while opening 001001 and those below.

The limits of this operation are clear:

1. you can have only 999 subfolders in each folder, but then again 999 in those. If you want more, change the structure to 0001 and exchange all "3" by "4" in the text above.

2. There is a limit to length of SQL statements in many servers. If the level becomes too deep, the SQL statement cannot be parsed anymore. Then you need to make separate queries while the levels are displayed to split the load.

Last Update: 2004-10-18 17:48:19
 FAQ > Programming  > General