欢迎光临
我们一直在努力

无限级别菜单的实现(其实还是有限级别的^0^)-PHP教程,PHP应用

建站超值云服务器,限时71元/月

<?  /* 看到很多朋友问过无限级别菜单的的问题(其实理论上还是有级别的,毕竟要受到个方便的条件的限制,比如: 数据库字段的类型等),我曾经用老大(唠叨)提供的代码写出来过无限级别的菜单,但是感觉效果不是很好(视觉上),于是趁着"夜深人静"就写这个"无限制级别的菜单",其实道理很简单,主要是数据表的设计,还有递归方法的使用(如果有时间我会用中值排序法来做),我会在下面给出数据结构的设计(非常简单),这里我没有加上竖直的虚线(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>                                                                                      
        <table id="dwtop" style="display=none" cellspacing="0" cellpadding="0">
         <?        
               $firstdepartment=$result[0];
               $basenum=strlen($firstdepartment)/3;//计算层数,其实这个有点多余,因为其必为第一层
               $searchpattern=$firstdepartment."___";    //设置查找条件       
               listchildtree($searchpattern,$basenum);        //显示祖宗节点的孩子节点
         ?>
        </table>
        </td>
         </tr>
        </table>
       </div>
      </td>
     </tr>
    </table>
       </center>
   </div>

</body>
</html>

<?/* 表结构的设计

由于是测试表设计得非常的简单:

create table test (
  id mediumint(8) unsigned not null auto_increment, #流水号
  departmentid varchar(100) not null default ,    #单位代号
  departmentname varchar(100) not null default ,  #单位名称
  key id (id)  
)

数据插入的代码我在这里就不那出来给大家了(很容易写,相信大家都能写出来)

数据表的规则为:

001为第一级(如果999个不够,请自行添加)
001001为001的第一个子节点,001002为001的第二个子节点
001001001为001001的第一个子节点,以此类推……

我这里只设置了一个"祖宗"(001),所以在程序中就直接调用了,可根据需要自己来设置,并对代码作简单的修改即可!

好了,就到这里了,如果大家有问题欢迎和我探讨!最好祝大家今天工作愉快!
先吸颗烟在睡觉!好累!(因为刚刚写了一个webftp,如果哪位兄弟姐妹需要请mail我)
*/

?>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 无限级别菜单的实现(其实还是有限级别的^0^)-PHP教程,PHP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章