- //这是一款由PHP+MysqL数据库的读取数据库的内容再生成树型号菜单由js来控制
- */
- ?>
- <html>
- <head>
- <Meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>类别目录树</title>
- <script type="text/网页特效">
- function showmenu(menuid)
- {
- if(menuid.style.display=="none"){
- menuid.style.display="";
- }
- else{
- menuid.style.display="none";
- }
- }
- </script>
- <style>
- body{margin:0px;}
- table tr td{font-size:12px}
- </style>
- </head>
- <body>
- <?PHP
- $globals["id"] =1; //用来跟踪下拉菜单的id号
- $layer=1; //用来跟踪当前菜单的级数
- //连接数据库
- $con=MysqL_connect("localhost","root","123456");
- MysqL_select_db("demo");
- //提取一级菜单
- $sql="select * from think_news where `f_id`=0";
- $result=MysqL_query($sql,$con);
- //如果一级菜单存在则开始菜单的显示
- if(MysqL_num_rows($result)>0) showtreemenu($con,$result,$id);
- function showtreemenu($con,$layer)
- {
- //取得需要显示的菜单的项目数
- $numrows=MysqL_num_rows($result);
- //开始显示菜单,每个子菜单都用一个表格来表示
- echo "<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
- for($rows=0;$rows<$numrows;$rows++)
- {
- //将当前菜单项目的内容导入数组
- $menu=MysqL_fetch_array($result);
- //提取菜单项目的子菜单记录集
- $sql="select * from think_news where f_id=$menu[id]";
- $result_sub=MysqL_query($sql,$con);
- echo "<tr>";
- //如果该菜单项目有子菜单,则添加javascript onclick语句
- if(MysqL_num_rows($result_sub)>0)
- {
- echo "<td width='20' valign= 'top' ><span onclick='showmenu(menu".$globals['id'].")' ><img src='menu_minus.gif' border='0' align='absmiddle'></span></td>";
- echo "<td class='menu' >";
- }
- else
- {
- echo "<td width='20'><img src='menu_plus.gif' border='0' align='absmiddle' > </td>";
- echo "<td class='menu'>";
- }
- //如果该菜单项目没有子菜单,只显示菜单名称
- echo $menu["title"];
- echo "</td></tr>";
- //如果该菜单项目有子菜单,则显示子菜单
- if(MysqL_num_rows($result_sub)>0)
- {
- //指定该子菜单的id和style,以便和onclick语句相对应
- echo "<tr id=menu".$globals["id"]++." style='display:none'>";
- echo "<td width='20'> </td>";
- echo "<td>";
- //将级数加1
- $layer++;
- //递归调用showtreemenu()函数,生成子菜单
- showtreemenu($con,$result_sub,$layer);
- //子菜单处理完成,返回到递归的上一层
- echo "</td></tr>";
- }
- //子菜单处理完成,返回到递归的上一层,将级数减1
- $layer--;
- }
- echo "</table>";
- }