Home > Article > Backend Development > Use PHP to implement Windows Explorer style tree menu_PHP tutorial
The following is a tree menu with a style similar to Windows Explorer
Include the following script into your page
In addition, you need to intercept some gif images from the Explorer, please see the comments in the script
The format of the menu structure file is:
tree level|item text|item link|link target|last item in subtree
For example
.Demo menu|javascript: alert(' This is the demo menu for TreeMenu 1.0');
..Directory 1
...Subdirectory 1.1
... .item 1.1.1|javascript: alert('Item 1.1.1');
....item 1.1.2|javascript: alert('Item 1.1.1');
...item 1.2 |javascript: alert('Item 1.2');
...item 1.3|javascript: alert('Item 1.3');
..Subdirectory 2
. ..item 2.1|javascript: alert('Item 2.1');
...item 2.2|javascript: alert('Item 2.2');
...Subdirectory 2.3 b>
....item 2.3.1|javascript: alert('Item 2.3.1');
....item 2.3.2|javascript: alert('Item 2.3.2');
// file:tremenu.inc
// PHP TreeMenu
//////////////////
// Initialization //
//////////////////
$treefile = "demomenu.txt";//Set a file with a menu structure
if(isset($ PATH_INFO))
$script = $PATH_INFO;
else
$script = $SCRIPT_NAME;
//The following pictures can be taken to capture the corresponding location in Windows Explorer
$img_expand = "tree_expand.gif"; //Shaped like an expandable node in the resource manager +
$img_collapse = "tree_collapse.gif"; //Shaped like an expanded node in the resource manager -
$img_line = "tree_vertline .gif";//Shaped like |
$img_split = "tree_split.gif";//Shaped like |-
$img_end = "tree_end.gif";//Shaped like L
$img_leaf = "tree_leaf.gif";//Shaped like o
$img_spc = "tree_space.gif";//Blank image
//The following is the menu processing script
$maxlevel=0;
$cnt =0;
$fd = fopen($treefile, "r");
if ($fd==0) die("treemenu.inc : Unable to open file ".$treefile);
while ($buffer = fgets($fd, 4096))
{
$tree[$cnt][0]=strspn($buffer,".");
$tmp=rtrim(substr( $buffer,$tree[$cnt][0]));
$node=explode("|",$tmp);
$tree[$cnt][1]=$node[0];
$tree[$cnt][2]=$node[1];