php级联菜单,树型结构
function tree(){
$Conn=mysql_connect("localhost","root","123456");
mysql_select_db("chess_game_db"); //打开数据库
mysql_query("set names utf8"); //避免乱码
echo '<style type="text/css">';
echo '.Menu { COLOR:#000000; FONT-SIZE: 12px; CURSOR: hand;}';
echo 'a{color:#000000;text-decoration:none;}';
echo '</style>';
echo '<script type="text/javascript">';
echo 'function ShowMenu(MenuID) ';
echo '{ ';
echo 'if(MenuID.style.display=="none") { MenuID.style.display=""; } else { MenuID.style.display="none"; } } </script>';
$GLOBALS["id"] =1;
$jibie=1;
$sql="select * from menu where parentid=0 AND is_del='0' AND id!=122121239 order by id asc";
$result=mysql_query($sql,$Conn);
echo "<table><tr><td colspan=2 height=30><a href=index.php?c=index&m=add_menu&action=add>添加一级栏目</a></td></tr></table>";
if(mysql_num_rows($result)>0)
$this->TreeMenu($Conn,$result,$jibie,$id);
}
function TreeMenu($Conn,$result,$jibie) //进入函数循环
{
$numrows=mysql_num_rows($result);
echo "<table border=0 cellpadding='0' cellspacing='0' width=100% style='border-top:1px solid #CCCCCC;'>";
for($rows=0;$rows<$numrows;$rows++)
{
$menu=mysql_fetch_array($result);
$sql="select * from menu where parentid=".$menu[id]." and is_del='0' order by id asc";
$result_sub=mysql_query($sql,$Conn);
echo "<tr height=30>";
//如果该菜单项目有子菜单,则添加JavaScript onClick语句
if(mysql_num_rows($result_sub)>0)
{
echo "<td width='30' style='border-top:1px solid #CCCCCC;cursor:pointer;' onclick='javascript:ShowMenu(Menu".$GLOBALS["ID"].");'><img src='images/+.gif' border='0'></td>";
echo "<td style='border-top:1px solid #CCCCCC;' class='Menu'>";
}
else
{
echo "<td width='30' style='border-top:1px solid #CCCCCC;'><img src='images/-.gif' border='0'></td>";
echo "<td style='border-top:1px solid #CCCCCC;' class='Menu'>";
}
if($menu
echo "<a href='$menu'>$menu[classname]</a> ";
else
if($jibie<=2){
echo "$menu[classname] <a href='index.php?c=index&m=add_menu&id={$menu[id]}&action=add'> <font color=green>添加</font></a> <a href='index.php?c=index&m=add_menu&id={$menu[id]}&action=edit'> <font color=blue>编辑</font></a> ";
}else{
echo "$menu[classname] <a href='index.php?c=index&m=add_menu&id={$menu[id]}&action=edit'> <font color=blue>编辑</font></a> ";
}
echo "<a onclick='javascript:return confirm(\"您确定要删除吗?\");' href='index.php?c=index&m=add_menu&id={$menu[id]}&action=del'> <font color=red>删除</font></a>";
echo "</td> </tr>";
if(mysql_num_rows($result_sub)>0)
{
echo "<tr id=Menu".$GLOBALS["ID"]++." style='display:none'>";
echo "<td width='30'> </td>";
echo "<td>";
//将级数加1
$jibie++;
$this->TreeMenu($Conn,$result_sub,$jibie);
$jibie--;
echo "</td></tr>";
}
//显示下一菜单
}
echo "</table>";
}