动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 无限级别菜单的实现(其实还是有限级别的^0^)

作者来源: 
阅读 1257 人次 , 2006-3-29 4:08: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>
<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我)
*/


?>

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:支持stmp认证、HTML格式邮件的类
· 下一篇:删除目录及其下文件的函数
· Php部分常见问题总结
· 用PHP实现登陆验证码(类似条行码状)
· 使用 php4 加速 web 传输
· 聊天室技术 - 密谈的实现
· 能把汉字转化为拼音的一个函数


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077