PHP instance: Using PHP to implement a Windows-style tree menu

Source: Internet
Author: User
Tags count explode implement resource

The following is a tree menu of styles similar to Windows Explorer, including the following script in your page, and you need to intercept some GIF pictures from the Explorer, 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

The following are the referenced contents:

. <b>demo Menu</b>|javascript:alert (' This are the Demo menu for Treemenu 1.0 ');
.. <b> Catalogue 1</b>
...<b> subdirectory 1.1</b>
... 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 ');
.. <b> Sub-Directory 2</b>
... item 2.1|javascript:alert (' Item 2.1 ');
... item 2.2|javascript:alert (' Item 2.2 ');
...<b> subdirectory 2.3</b>
... item 2.3.1|javascript:alert (' Item 2.3.1 ');
... item 2.3.2|javascript:alert (' Item 2.3.2 ');
<?php
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 picture can intercept the map of the corresponding location in Windows Explorer

$img _expand = "tree_expand.gif";//form as Extensible node in Resource Manager +
$img _collapse = "tree_collapse.gif";//in the form of an extended node in 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";//form as O
$img _SPC = "tree_space.gif";//Blank chart

The following is a 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];
$tree [$cnt][3]= $node [2];
$tree [$cnt][4]=0;
if ($tree [$cnt][0] > $maxlevel) $maxlevel = $tree [$cnt][0];
$cnt + +;
}
Fclose ($FD);

for ($i =0; $i <count ($tree); $i + +)
{
$expand [$i]=0;
$visible [$i]=0;
$levels [$i]=0;
}

if ($p!= "")
$explevels = Explode ("|", $p);
$i = 0;
while ($i <count ($explevels))
{
$expand [$explevels [$i]]=1;
$i + +;
}

$lastlevel = $maxlevel;
For ($i =count ($tree)-1; $i >=0; $i--)
{
if ($tree [$i][0] < $lastlevel)
for ($j = $tree [$i][0]+1; $j <= $maxlevel; $j + +)
$levels [$j]=0;
if ($levels [$tree [$i][0]]==0)
{
$levels [$tree [$i][0]]=1;
$tree [$i][4]=1;
}
Else
$tree [$i][4]=0;
$lastlevel = $tree [$i][0];
}

for ($i =0; $i < count ($tree); $i + +)
if ($tree [$i][0]==1)
$visible [$i]=1;
for ($i =0; $i < count ($explevels); $i + +)
{
$n = $explevels [$i];
if ($visible [$n]==1) && ($expand [$n]==1)]
{
$j = $n +1;
while ($tree [$j][0] > $tree [$n][0])
{
if ($tree [$j][0]== $tree [$n][0]+1) $visible [$j]=1;
$j + +;
}
}
}

for ($i =0; $i < $maxlevel; $i + +)
$levels [$i]=1;

$maxlevel + +;

echo "<table cellspacing=0 cellpadding=0 border=0 cols=". ($maxlevel +3). " >\n ";
echo "<tr>";
for ($i =0; $i < $maxlevel; $i + +)
echo "<td width=16></td>";
echo "<td width=100%> </td></tr>\n";
$cnt = 0;
while ($cnt <count ($tree))
{
if ($visible [$cnt])
{
echo "<tr>";
$i = 0;
while ($i < $tree [$cnt][0]-1)
{
if ($levels [$i]==1)
echo "<td><a name= ' $cnt ' ></a></td>";
Else
echo "<td><a name= ' $cnt ' ></a></td>";
$i + +;
}
if ($tree [$cnt][4]==1)
{
echo "<td></td>";
$levels [$tree [$cnt][0]-1]=0;
}
Else
{
echo "<td></td>";
$levels [$tree [$cnt][0]-1]=1;
}

if ($tree [$cnt +1][0]> $tree [$cnt][0])
{

$i = 0; $params = "p=";
while ($i <count ($expand))
{
if ($expand [$i]==1) && ($cnt!= $i) | | ($expand [$i]==0 && $cnt = = $i))
{
$params = $params. $i;
$params = $params. "|";
}
$i + +;
}

if ($expand [$cnt]==0)
echo "<td><a href=\" ". $script. $params." # $cnt \ "></a></td>";
Else
echo "<td><a href=\" ". $script. $params." # $cnt \ "></a></td>";
}
Else
echo "<td></td>";

if ($tree [$cnt][2]== "")
echo "<td colspan=". ($maxlevel-$tree [$cnt][0]). " > ". $tree [$cnt][1]." </td> ";
Else
echo "<td colspan=". ($maxlevel-$tree [$cnt][0]). " ><a href=\ "". $tree [$cnt][2]. " \ "Target=\" ". $tree [$cnt][3]." \ > $tree [$cnt][1]. " </a></td> ";
echo "</tr>\n";
}
$cnt + +;
}
echo "</table>\n";
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.