Unlimited menu implementation

Source: Internet
Author: User

<? /* Many of my friends have asked questions about the unlimited menu (in theory, there are still some levels. After all, they are subject to convenient restrictions, such as the type of database fields ), I used the code provided by the boss (nagging) to write unlimited menus, but I felt that the effect was not very good (visually ), therefore, taking advantage of "quiet night", we can write this "Unlimited menu". In fact, the principle is very simple, mainly because of the design of data tables, there is also the use of recursive methods (if there is time I will use the value sorting method), I will give the data structure design below (very simple ), here I didn't add a vertical dotted line (windows Resource Manager dotted line), and I fixed the SQL statement. You can modify it as needed! If you have any questions can contact me: msn: banneryue@sina.com, QQ: 7665656, E_mail: yuepengfei@mail.banner.com.cn

Tomorrow (today, huh, huh) I will provide a test page for you to see (because I can only dial up the Internet in the dormitory and the IP address is not fixed)

*/

/** Recursively display subnode Functions
*
*
* @ Param $ Search Condition (like)
* @ Param $ number of layers of the BaseNum Node
*/

Function ListChildTree ($ SearchPattern, $ BaseNum ){
Global $ Tree; // declare that the database connection handle is global.
$ SQL = "select distinct mentid, DepartmentName from test where distinct mentid like '$ SearchPattern'"; // search for Child Nodes
$ QueryChild = $ Tree-> query ($ SQL );
While ($ Result = $ Tree-> fetch_array ($ QueryChild) {// retrieve the child node
$ Space = "";
For ($ j = 0; $ j <(strlen ($ SearchPattern)/3)-$ BaseNum); $ j ++)
$ Space. = ""; // set the distance before the display node. Here, the Space html is automatically replaced ""
$ ChildDepartment = trim ($ Result [0]). "___";
$ ChildSql = "select count (*) from test where perform mentid like '$ ChildDepartment'"; // you can find the child node of a child node.
$ ChildResult = $ Tree-> query_first ($ ChildSql );
$ TableId = "ta". trim ($ Result [0]); // you can specify a table Id.
$ TablePic = "ta". trim ($ Result [0]). "pic"; // set the image Id
If ($ ChildResult [0] <1) {// if the node of the child node is not found, the "-" image is displayed.
?>
<Tr> <td> <? = $ Space?> <Span align = "absmiddle"> </span> <font size = "2"> <A href = "process. php? SearchPattern = <? = Trim ($ Result [0])?> "Class =" F1 "> <? = $ Result [1]?> </A> </font>
<Table id = "<? = $ TableId?> "Style =" display = none "cellspacing =" 0 "cellpadding =" 0 ">

<?} Else {// The "+" image will be displayed if it is found
?>
<Tr> <td> <? = $ Space?> <A onclick = "javascript: expands ('<? = $ TableId?> ',' <? = $ TablePic?> ') "Style =" cursor: hand "> <span align =" absmiddle "> "Src =" parent.gif "border =" 0 "align =" absmiddle "width =" 35 "height =" 17 "> </span> </a> <font size =" 2 "> <A href =" process. php? SearchPattern = <? = Trim ($ Result [0])?> "Class =" F1 "> <? = $ Result [1]?> </A> </font>
<Table id = "<? = $ TableId?> "Style =" display = none "cellspacing =" 0 "cellpadding =" 0 ">
<?
ListChildTree ($ ChildDepartment, $ BaseNum); // recursively call the function itself to display other child nodes
} // End if?>
</Table>
<?} // End while
} // End function?>
<Html>
<Head>
<Title> unlimited menu test </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Link rel = "stylesheet" href = "../text.css" type = "text/css">
<Script language = "javascript">
Function expands (expid, picid) // display the Js
{// Alert ("this.doc ument. all [" + expid + "]. style. display ");
If(this.doc ument. all [expid]. style. display = "none ")
{This.doc ument. all [expid]. style. display = "block ";
This.doc ument. all [picid]. src = "leaf.gif ";

}
Else
{
This.doc ument. all [expid]. style. display = "none ";
This.doc ument. all [picid]. src = "parent.gif ";
}
}
</Script>
</Head>

<Body bgcolor = "# FFFFFF" text = "#000000">
<?
Require ("do_mySql.php ");
$ Tree = new DB_ SQL;
$ Tree-> connect (); // connect to the database. You can change it to your own code as needed.

$ SQL = "select parent mentid, DepartmentName from test where length (parent mentid) = 3"; // you can modify the parent node as needed.
$ Result = $ Tree-> query_first ($ SQL );
?>
<Div align = "center">
<Center>
<Table border = "1" cellpadding = "0" cellspacing = "0" width = "766" bordercolor = "# DDCF90" height = "392">
<Tr>
<Td valign = "top">
<Div align = "center">
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "372">
<Tr>
<Td width = "368"> <a onclick = "javascript: expands ('dwtop ', 'dwimg')" style = "cursor: hand "> <span align =" absmiddle "> </span> </a> <font size = "2"> <a href = "process. php? SearchPattern = <? = $ Result [0]?> "> <? = $ Result [1]?> </A> </font>
<Table id = "dwtop" style = "display = none" cellspacing = "0" cellpadding = "0">
<?
$ FirstDepartment = $ Result [0];
$ BaseNum = strlen ($ FirstDepartment)/3; // Number of computing layers. In fact, this is a bit redundant because it must be the first layer.
$ SearchPattern = $ FirstDepartment. "___"; // you can specify search criteria.
ListChildTree ($ SearchPattern, $ BaseNum); // displays the child node of the ancestor Node
?>
</Table>
</Td>
</Tr>
</Table>
</Div>
</Td>
</Tr>
</Table>
</Center>
</Div>

</Body>
</Html>

<? /* Table Structure Design

The design of the test table is very simple:

Create table test (
Id mediumint (8) unsigned not null auto_increment, # serial number
DepartmentId varchar (100) not null default '', # unit code
DepartmentName varchar (100) not null default '', # unit name
KEY id (id)
)

I won't write the data insertion code here (it's easy to write, I believe everyone can write it)

The data table rules are as follows:

001 is the first level (if 999 is not enough, please add it yourself)
001001 is the first subnode of 001, and 001002 is the second subnode of 001.
001001001 is the first subnode of 001001, and so on ......

I only set one "ancestor" (001) Here, so it is called directly in the program. You can set it as needed and make simple modifications to the code!

Okay, that's it. If you have any questions, please discuss them with me! I 'd like to wish you a pleasant job today!
Smoke in bed first! Tired! (Because I have just written a webFtp file. If any brother or sister needs to mail me)
*/


?>

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.