Made a few days to base things, today to a little bit difficult, generally in the background of the CMS system will be used in the category of management of the essence--Unlimited class menu, for beginners, this may have a certain degree of difficulty, but today after listening to my thin road, I believe that everyone will get this stuff. How to achieve it? Let's make a database first:
--
--The structure of the table ' Cr_columninfo '
--
Copy Code code as follows:
CREATE TABLE ' Cr_columninfo ' (
' ColumnID ' int (4) not NULL auto_increment,
' Columnfatherid ' int (4) Not NULL default ' 0 ',
' columnname ' varchar not NULL default ',
' Columnadder ' varchar not NULL default ',
' Columninputdate ' date not NULL default ' 0000-00-00 ',
PRIMARY KEY (' ColumnID ')
) Engine=myisam auto_increment=15 DEFAULT Charset=utf8 auto_increment=15;
--Export the data in the table ' Cr_columninfo '
--
INSERT into ' cr_columninfo ' (' ColumnID ', ' Columnfatherid ', ' columnname ', ' columnadder ', ' columninputdate ') VALUES (1, 0, ' AV star ', ' Leehui ', ' 2006-09-28 ',
(2, 0, ' campus style ', ' leehui1983 ', ' 2006-09-28 '),
(3, 1, ' Hong Kong and Taiwan Stars ', ' Leehui ', ' 2006-09-28 '),
(4, 0, ' Landscape picture ', ' leehui1983 ', ' 2006-09-29 '),
(5, 4, ' vast sea ', ' leehui1983 ', ' 2006-09-29 '),
(6, 5, ' Fuzhou's Sea ', ' leehui1983 ', ' 2006-09-29 '),
(7, 2, ' Graduation picture ', ' Leehui ', ' 2006-09-29 '),
(9, 0, ' sports star ', ' leehui1983 ', ' 2006-10-02 '),
(10, 0, ' exquisite wallpaper ', ' leehui1983 ', ' 2006-10-02 '),
(11, 0, ' City scenery ', ' leehui1983 ', ' 2006-10-02 '),
(12, 0, ' cartoon anime ', ' leehui1983 ', ' 2006-10-02 '),
(13, 0, ' Screenshot of Game ', ' leehui1983 ', ' 2006-10-02 '),
(14, 0, ' author albums ', ' leehui1983 ', ' 2006-10-02 ');
These are test data, the basic principle is to build data fields according to the tree structure, the core is the column has its own ID number and the ID number of the parent column, depending on these two relations, the establishment of tree structure, top column Father Id=0, this is good understanding, now to describe the implementation of the principle This program uses the most common recursive algorithm to traverse the submenu, first, query out all top-level menus, display in a large table, no line to display a top-level menu, and then recursively list all submenus, submenus in the parent menu in the row of the next line, and the line display property is not displayed, The line ID number generated by the program dynamically, combined with the display and hide of JS control line, which is similar to the scalability effect of Microsoft menu, the article finally has a picture to help you understand, this is to generate the HTML page to copy into the DW to just demonstrate the final results of the program.
Look at the Code section, and there is no difficult to understand the syntax, please use the annotation to read, you are interested to extend this code.
The PHP code is as follows:
Copy Code code as follows:
<meta http-equiv= "Content-type" C/>
<title> Categories Directory tree </title>
<script type= "Text/javascript" >
function ShowMenu (MENUID)
{
if (menuid.style.display== "None") {
Menuid.style.display= "";
}
else{
Menuid.style.display= "None";
}
}
</script>
<link href= "Style.css" rel= "stylesheet" type= "Text/css" >
<body topmargin= "0" bgcolor= "#EFEFE7" >
<table width= "100%" height= "border=" 0 "cellpadding=" 0 "cellspacing=" 0 "bgcolor=" #739E18 ">
<tr>
<TD align= "left" > <strong> column tree structure list </strong></td>
</tr>
</table>
<?php
Basic variable Settings
$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", "7529639");
mysql_select_db ("Cr_download");
mysql_query ("SET NAMES ' GBK '");
Extract A Level menu
$sql = "SELECT * from Cr_columninfo where columnfatherid=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);
//=============================================
Show tree menu Functions Showtreemenu ($con, $result, $layer)
$con: Database connections
$result: Menu Recordset to display
$layer: The series of menus that need to be displayed
//=============================================
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 Cr_columninfo where columnfatherid= $menu [ColumnID]";
$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= ' > </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 [ColumnName];
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>";
}
?>
</body>
Finally paste the effect diagram and source code packaging, I hope this article is helpful to everyone ^_^
Download this file