<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> <title> Categories Directory tree </title> <script type= "text/web Effects" > 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> <body> <?php $globals ["id"] = 1; The ID number used to track the Drop-down menu $layer = 1; The series used to track the current menu Connecting to a database $con =mysql_connect ("localhost", "root", "123456"); mysql_select_db ("demo"); Extract A Level menu $sql = "SELECT * from Think_news where ' f_id ' = 0"; $result =mysql_query ($sql, $con); If a level menu exists, the Start menu is displayed if (mysql_num_rows ($result) >0) Showtreemenu ($con, $result, $id); function Showtreemenu ($con, $result, $layer) { Number of items to get the menu to display $numrows =mysql_num_rows ($result); Start displaying menus, each of which is represented by a table echo "<table cellpadding= ' 0 ' cellspacing= ' 0 ' border= ' 0 ' width= ' 100% ' >"; for ($rows =0; $rows < $numrows; $rows + +) { Import the contents of the current menu item into an array $menu =mysql_fetch_array ($result); Extracts a submenu record set for a menu item $sql = "SELECT * from Think_news where f_id= $menu [id]"; $result _sub=mysql_query ($sql, $con); echo "<tr>"; If the menu item has a submenu, add the JavaScript onclick statement if (mysql_num_rows ($result _sub) >0) { echo "<td width= ' valign= ' top ' ><span onclick= ' ShowMenu" (menu). $globals [' id ']. ") ' ></span></td> '; echo "<td class= ' menu ' >"; } Else { echo "<td width= ' > </td>"; echo "<td class= ' menu ' >"; } If the menu item does not have a submenu, only the menu name is displayed echo $menu ["title"]; echo "</td></tr>"; If the menu item has a submenu, the submenu is displayed if (mysql_num_rows ($result _sub) >0) { Specify the ID and style of the submenu to correspond to the onclick statement echo "<tr id=menu". $globals ["id"]++. "style= ' Display:none ' >"; echo "<td width= ' > </td>"; echo "<td>"; Add 1 to the series $layer + +; Recursively call the Showtreemenu () function to generate a submenu Showtreemenu ($con, $result _sub, $layer); submenu processing complete, return to the previous level of recursion echo "</td></tr>"; } submenu processing complete, return to the upper level of recursion, reduce the series by 1 $layer--; } echo "</table>"; } |