This paper describes the implementation method of Php+js three menu linkage menu. Share to everyone for your reference, as follows:
<title>Smart recursive menu-read database</title> <?php//$Id: $//basic Variable setting $globals["id"] = 1;//used to track the Id number of the drop-down menu $layer =1;//is used to track the current menu's progression//connection to the database $con=mysql_connect (" LocalHost "," root "," 123456 "); mysql_select_db ("menu");//Extract the first-level menu $sql= "SELECT * from menu where parent_id=0"; $result =mysql_query ($sql, $Con);// If the first level menu exists then the Start menu displays if (mysql_num_rows ($result) >0) Showtreemenu ($Con, $result, $layer, $ID); =============================================//Display Tree menu function Showtreemenu ($con, $result, $layer)//$con: Database connection//$ Result: The menu Recordset that needs to be displayed//layer: The progression of the menu that needs to be displayed//=============================================functionshowtreemenu ($Con, $ result, $layer) {//Get the number of items that need to be displayed for the menu $numrows=mysql_num_rows ($result); Start to display the menu, each submenu is represented by a table echo "< tablecellpadding = ' 0 ' cellspacing = ' 0 ' border = ' 0 ' >"; for ($rows =0; $rows < $numrows; $rows + +) {//Import the contents of the current menu item into an array $menu =mysql_fetch_array ($result); Extracts the menu item's submenu recordset $sql = "SELECT * frommenuwhereparent_id = $menu [id]"; $result _sub=mysql_query ($sql, $Con); echo "< tr >"; If the menu item has submenus, add JavaScript onclicK Statement if (mysql_num_rows ($result _sub) > 0) {echo "< Tdwidth = '" > < IMGSRC = ' tree_expand.gif ' border = ' 0 ' > </td > "; echo "< Tdclass = ' Menu ' OnClick = ' Javascript:showmenu (menu". $GLOBALS ["ID"]. "); ' > ';} else {echo ' < Tdwidth = ' > ' < IMGSRC = ' tree_collapse.gif ' border = ' 0 ' > </td > '; echo "< Tdclass = ' menu ' >";} If the menu item does not have a submenu and the hyperlink address is specified, the hyperlink is specified,//otherwise only the menu name is displayed if ($menu [url]!= "] echo" < Ahref = ' $menu [url] ' > $men U[name] </a > "; else echo $menu [name]; echo "</td > </TR >"; If the menu item has a submenu, display the submenu if (mysql_num_rows ($result _sub) >0) {//Specify the ID and style of this submenu so that the onclick statement corresponds to echo "< Trid = Menu ". $GLOBALS [" ID "]++." > "; echo ""; echo ""; Increase the progression by 1 $layer + +; Recursively call the Showtreemenu () function to generate a submenu Showtreemenu ($Con, $result _sub, $layer); Sub-menu processing is completed, return to the previous level of recursion, the progression minus 1 $layer-; echo "</td > </TR >"; }//Continue to display the next menu item} echo "</table >";}? > <?php $id = 1; function test () {global $id; unset ($id); } test (); echo "< Fontclass = menu >". ($id). "</font >"; In PHP 4 This will print out 1?> <?php $a = 1; $b = 2; function Sum () {global $a, $b; $b =$ A + $b; } Sum (); echo "< Fontclass = menu >". $b. "</font >"; ?>
More readers interested in PHP related content can view the topic: "PHP Primer for Object-oriented programming", "PHP String Usage Summary", "Php+mysql Database Operations Primer" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
The above describes the Php+js three-level menu Linkage menu implementation method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.