PHP+JS三级菜单联动菜单实现方法
程序员文章站
2023-12-03 17:29:16
本文实例讲述了php+js三级菜单联动菜单实现方法。分享给大家供大家参考,具体如下:
<...
本文实例讲述了php+js三级菜单联动菜单实现方法。分享给大家供大家参考,具体如下:
<html> <head> <title> 智能递归菜单-读取数据库 </title> <style> td { font-family: "verdana", "宋体"; font-size: 12px; line-height: 130%; letter-spacing:1px } a:link { color: #990000; font-family: "verdana", "宋体"; font-size: 12px; text-decoration: none; letter-spacing:1px } a:visited { color: #990000; font-family: "verdana", "宋体"; font-size: 12px; text-decoration: none; letter-spacing:1px } a:active { color: #990000; font-family: "verdana", "宋体"; font-size: 12px; text-decoration: none; letter-spacing:1px } a:hover { color: #ff0000; font-family: "verdana", "宋体"; font-size: 12px; text-decoration: underline; letter-spacing:1px } .menu { color:#000000; font-family: "verdana", "宋体"; font-size: 12px; cursor: hand } </style> <script language=javascript> function showmenu(menuid) { if (menuid.style.display == "none") { menuid.style.display = ""; } else { menuid.style.display = "none"; } } </script> </head> <body> <?php // $id:$ //基本变量设置 $globals["id"] = 1; //用来跟踪下拉菜单的id号 $layer=1; //用来跟踪当前菜单的级数 //连接数据库 $con=mysql_connect( "localhost", "root", "123456"); mysql_select_db( "menu"); //提取一级菜单 $sql="select * from menu where parent_id=0" ; $result=mysql_query($sql,$con); //如果一级菜单存在则开始菜单的显示 if(mysql_num_rows($result)>0) showtreemenu($con, $result, $layer, $id); //============================================= //显示树型菜单函数 showtreemenu($con,$result,$layer) //$con:数据库连接 //$result:需要显示的菜单记录集 //layer:需要显示的菜单的级数 //============================================= function showtreemenu($con, $result, $layer) { //取得需要显示的菜单的项目数 $numrows=mysql_num_rows($result); //开始显示菜单,每个子菜单都用一个表格来表示 echo " < tablecellpadding = '0'cellspacing = '0'border = '0' > "; for($rows=0;$rows <$numrows;$rows++) { //将当前菜单项目的内容导入数组 $menu=mysql_fetch_array($result); //提取菜单项目的子菜单记录集 $sql="select * frommenuwhereparent_id = $menu[id]" ; $result_sub=mysql_query($sql,$con); echo " < tr > "; //如果该菜单项目有子菜单,则添加javascript onclick语句 if(mysql_num_rows($result_sub)> 0) { echo " < tdwidth = '20' > < imgsrc = 'tree_expand.gif'border = '0' > < / td > "; echo " < tdclass = 'menu'onclick = 'javascript:showmenu(menu".$globals["id"].");' > "; } else { echo " < tdwidth = '20' > < imgsrc = 'tree_collapse.gif'border = '0' > < / td > "; echo " < tdclass = 'menu' > "; } //如果该菜单项目没有子菜单,并指定了超级连接地址,则指定为超级连接, //否则只显示菜单名称 if($menu[url]!="") echo " < ahref = '$menu[url]' > $menu[name] < / a > "; else echo $menu[name]; echo " < / td > < / tr > "; //如果该菜单项目有子菜单,则显示子菜单 if(mysql_num_rows($result_sub)>0) { //指定该子菜单的id和style,以便和onclick语句相对应 echo " < trid = menu".$globals["id "]++ . " style='display:none'>"; echo "<td width='20'></td>"; echo "<td>"; //将级数加1 $layer++; //递归调用showtreemenu()函数,生成子菜单 showtreemenu($con,$result_sub,$layer); //子菜单处理完成,返回到递归的上一层,将级数减1 $layer--; echo "< / td > < / tr > "; } //继续显示下一个菜单项目 } echo " < / table > "; } ?> <?php $id=1 ; function test() { global $id; unset($id); } test(); echo " < fontclass = menu > ".($id). " < / font > "; // 在 php 4 中这里会打印出 1 ?> <?php $a=1 ; $b=2 ; function sum() { global $a, $b; $b=$ a + $b; } sum(); echo " < fontclass = menu > ".$b. " < / font > "; ?> </body> </html>
更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。