PHP+MYSQL+JS implements the tree menu code

Source: Internet
Author: User
The code is as follows Copy Code

<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>";
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.