本文實例講述了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程序設計有所幫助。