打印本文 打印本文  关闭窗口 关闭窗口
无限级别菜单的实现
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:36:00
<?  /* 看到很多朋友问过无限级别菜单的的问题(其实理论上还是有级别的,毕竟要受到个方便的条件的限制,比如: 数据库字段的类型等),我曾经用老大(唠叨)提供的代码写出来过无限级别的菜单,但是感觉效果不是很好(视觉上),于是趁着"夜深人静"就写这个"无限制级别的菜单",其实道理很简单,主要是数据表的设计,还有递归方法的使用(如果有时间我会用中值排序法来做),我会在下面给出数据结构的设计(非常简单),这里我没有加上竖直的虚线(windows资源管理器的虚线),同时Sql语句我也将其固定,大家可以根据自己的需要来修改!如果有问题可以联系我:msn:banneryue@sina.com,QQ:7665656,E_mail:yuepengfei@mail.banner.com.cn

明天(已经是今天了,呵呵)我会提供一个测试页面让大家来看(因为我在宿舍只能拨号上网,Ip地址不固定)

*/

/** 递归显示子节点函数
*
*
* @param $SearchPattern    查找的条件(like)
* @param $BaseNum 节点的层数
*/

           function ListChildTree($SearchPattern,$BaseNum){
               global $Tree;//声明连接数据库的句柄为全局
               $Sql="select DepartmentId,DepartmentName from test where DepartmentId like '$SearchPattern'";    //查找孩子节点
               $QueryChild=$Tree->query($Sql);          
               while($Result=$Tree->fetch_array($QueryChild)) { //取出孩子节点
                   $Space="";
                    for($j=0;$j<((strlen($SearchPattern)/3)-$BaseNum);$j++)
                      $Space.="  ";                 //设置显示节点前面的距离,这里的空格的html被这里自动替换成"  "了
                   $ChildDepartment=trim($Result[0])."___";            
                   $ChildSql="select count(*) from test where DepartmentId like '$ChildDepartment'";//查找孩子节点的孩子节点
                   $ChildResult=$Tree->query_first($ChildSql);             
                   $TableId="ta".trim($Result[0]); //设置表格Id
                   $TablePic="ta".trim($Result[0])."pic";    //设置图片Id                   
                   if($ChildResult[0]<1){//如果没有找到孩子节点的节点,则显示"-"图片
                      ?>
                    <tr><td><?=$Space?><span align="absmiddle"><img src="leaf.gif" border="0" align="absmiddle" width="35" height="17"></span><font size="2"><A href="process.php?SearchPattern=<?=trim($Result[0])?>" class="F1"><?=$Result[1]?></a></font>
                    <table id="<?=$TableId?>" style="display=none" cellspacing="0" cellpadding="0">
                
                 <?}else{           //找到则显示"+"图片            
                  ?>
                   <tr><td><?=$Space?><a onclick="javascript:expands('<?=$TableId?>','<?=$TablePic?>')" style="cursor:hand"><span align="absmiddle"><img id="<?=$TablePic?>" src="parent.gif" border="0" align="absmiddle" width="35" height="17"></span></a><font size="2"><A href="process.php?SearchPattern=<?=trim($Result[0])?>" class="F1"><?=$Result[1]?></a></font>
                  <table id="<?=$TableId?>" style="display=none" cellspacing="0" cellpadding="0">
            <?
              ListChildTree($ChildDepartment,$BaseNum);//递归调用函数本身来显示其他孩子节点
            }//end if?>
             </table>
            <?}//end while
           }//end function?>
<html>
<head>
<title>无限级菜单测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../text.css" type="text/css">
<script language="javascript">
function expands(expid,picid) //显示图片张合的Js
{   //    alert("this.document.all["+expid+"].style.display");
  if(this.document.all[expid].style.display=="none")
  { this.document.all[expid].style.display="block";
    this.document.all[picid].src="leaf.gif";

  }
  else
  {
    this.document.all[expid].style.display="none";
    this.document.all[picid].src="parent.gif";
  }
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<?
  require("do_mySql.php");
$Tree = new DB_Sql;
$Tree->connect();//连接数据库,可根据需要换成自己的代码

  $Sql="select DepartmentId,DepartmentName from test where length(DepartmentId)=3";//提出最上层节点(祖宗节点),根据需要自己修改
  $Result=$Tree->query_first($Sql);
?>
<div align="center">                         
  <center>                         
  <table border="1" cellpadding="0" cellspacing="0" width="766" bordercolor="#DDCF90" height="392">                         
    <tr>                         
      <td valign="top">                     
        <div align="center">         
          <table border="0" cellpadding="0" cellspacing="0" width="372">         
            <tr>         
              <td width="368"><a onclick="javascript:expands('dwtop','dwimg')" style="cursor:hand"><span align="absmiddle"> <img id="dwimg" SRC="parent.gif" border="0" align="absmiddle" width="35" height="17"></span></a><font size="2"><a href="process.php?SearchPattern=<?=$Result[0]?>"><?=$Result[1]?></a></font>                 &

[1] [2]  下一页



打印本文 打印本文  关闭窗口 关闭窗口