PHP实例:用PHP实现windows风格的树型菜单

作者:网络 来源:佚名 更新时间:2008-12-06 03:39:24 点击:

以下是一个风格类似windows资源管理器的树型菜单,将下面的脚本包含到你的页面中,另外需要从资源管理器中截取一些gif图片,祥见脚本中的注释,菜单结构文件的格式为:

tree level|item text|item link|link target|last item in subtree

例如

以下为引用的内容:

.<b>demo menu</b>|javascript: alert('this is the demo menu for treemenu 1.0');
..<b>目录1</b>
...<b>子目录 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>子目录 2</b>
...item 2.1|javascript: alert('item 2.1');
...item 2.2|javascript: alert('item 2.2');
...<b>子目录 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
/////////////////
// 初始化 //
/////////////////

$treefile = "demomenu.txt";//设置有菜单结构的文件

if(isset($path_info))
$script = $path_info;
else
$script = $script_name;

//以下图片可截取windows资源管理器中的相应位置的图

$img_expand = "tree_expand.gif";//形如资源管理器中可扩展节点+
$img_collapse = "tree_collapse.gif";//形如资源管理器中已扩展节点-
$img_line = "tree_vertline.gif";//形如|
$img_split = "tree_split.gif";//形如|-
$img_end = "tree_end.gif";//形如l
$img_leaf = "tree_leaf.gif";//形如o
$img_spc = "tree_space.gif";//空白图

//以下是菜单处理脚本

$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%>&nbsp;</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><img src=\"".$img_line."\"></td>";
else
echo "<td><a name='$cnt'></a><img src=\"".$img_spc."\"></td>";
$i++;
}
if ($tree[$cnt][4]==1)
{
echo "<td><img src=\"".$img_end."\"></td>";
$levels[$tree[$cnt][0]-1]=0;
}
else
{
echo "<td><img src=\"".$img_split."\"></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\"><img src=\"".$img_expand."\" border=no></a></td>";
else
echo "<td><a href=\"".$script.$params."#$cnt\"><img src=\"".$img_collapse."\" border=no></a></td>";
}
else
echo "<td><img src=\"".$img_leaf."\"></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";
?>