Home >php教程 >php手册 >PHP实例:用PHP实现windows风格的树型菜单

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

WBOY
WBOYOriginal
2016-06-21 08:57:40809browse

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

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

例如

以下为引用的内容:

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

$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{
$expand[$i]=0;
$visible[$i]=0;
$levels[$i]=0;
}

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

$lastlevel=$maxlevel;
for ($i=count($tree)-1; $i>=0; $i--)
{
if ( $tree[$i][0] for ($j=$tree[$i][0]+1; $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 if ($tree[$i][0]==1)
$visible[$i]=1;
for ($i=0; $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$levels[$i]=1;

$maxlevel++;

echo "

\n";
echo "";
for ($i=0; $iecho "";
echo "\n";
$cnt=0;
while ($cnt{
if ($visible[$cnt])
{
echo "";
$i=0;
while ($i{
if ($levels[$i]==1)
echo "";
else
echo "";
$i++;
}
if ($tree[$cnt][4]==1)
{
echo "";
$levels[$tree[$cnt][0]-1]=0;
}
else
{
echo "";
$levels[$tree[$cnt][0]-1]=1;
}

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

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

if ($expand[$cnt]==0)
echo "

";
else
echo "";
}
else
echo "";

if ($tree[$cnt][2]=="")
echo "

";
else
echo "";
echo "\n";
}
$cnt++;
}
echo "
 
PHP实例:用PHP实现windows风格的树型菜单 PHP实例:用PHP实现windows风格的树型菜单 PHP实例:用PHP实现windows风格的树型菜单PHP实例:用PHP实现windows风格的树型菜单 PHP实例:用PHP实现windows风格的树型菜单PHP实例:用PHP实现windows风格的树型菜单PHP实例:用PHP实现windows风格的树型菜单 ".$tree[$cnt][1]."".$tree[$cnt][1]."
\n";
?>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn