Implementation of unlimited-level menus (in fact, a limited-level ^0^)

Source: Internet
Author: User
Tags dashed line mail modify connect sql net strlen trim
Menu.? * * See a lot of friends asked the unlimited level of the menu problem (in fact, there is a theoretical level, after all, to be subject to a convenient condition, such as: The type of database fields, etc., I used to use the Boss (nagging) to provide the code to the unlimited level of the menu, but the feeling is not very good (visual), so take advantage of " In the dead of Night "to write this" unrestricted-level menu, the truth is very simple, mainly the design of the datasheet, and the use of recursive methods (if there is time I will use the median ordering method to do), I will give the following data structure design (very simple), Here I do not add a vertical dotted line (the Windows Explorer dashed line), while the SQL statement I also fixed it, we can according to their own needs to modify! If you have any questions you can contact me: msn:banneryue@sina.com,qq:7665656,e_mail:yuepengfei@mail.banner.com.cn

Tomorrow (already today, hehe) I will provide a test page for everyone to see (because I can only dial the Internet in the dormitory, IP address is not fixed)

*/

/** recursive display of child node functions
*
*
* Conditions for @param $SearchPattern lookup (like)
* @param the number of layers $BaseNum nodes
*/

function Listchildtree ($SearchPattern, $BaseNum) {
Global $Tree;//declaring the handle of the connection database to be globally
$SQL = "Select Departmentid,departmentname from Test where DepartmentID like ' $SearchPattern '"; Find children nodes
$QueryChild = $Tree->query ($SQL);
while ($Result = $Tree->fetch_array ($QueryChild)) {//Remove child node
$Space = "";
For ($j =0 $j < ((strlen ($SearchPattern)/3)-$BaseNum); $j + +)
$Space. = ""; Sets the distance from the front of the display node, where the HTML for the space is automatically replaced by "".
$ChildDepartment =trim ($Result [0]). " ___";
$CHILDSQL = "SELECT count (*) from test where DepartmentID like ' $ChildDepartment '";//child node to find child node
$ChildResult = $Tree->query_first ($CHILDSQL);
$TableId = "Ta". Trim ($Result [0]); Set the table ID
$TablePic = "Ta". Trim ($Result [0]). "    Pic "; Set Picture ID
if ($ChildResult [0]<1) {//If the node of the child node is not found, the "-" picture 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{//Find Show "+" picture
?>
<tr><td><?= $Space? ><a onclick= "Javascript:expands (' <?= $TableId?> ', ' <?= $TablePic? > ') "style=" Cursor:hand "><span align=" absmiddle "> "src=http:// "Parent.gif" border= "0" align= "absmiddle" width= "www.163design.net/p/b/" height= "></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);//recursive call function itself to show other child nodes
}//end if?>
</table>
;?} End While
}//end function?>
<title> Unlimited-level 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 picture Zhang He JS
{//Alert ("this.document.all[" +expid+ "].style.display");
if (this.document.all[expid].style.display== "None")
{this.document.all[expid].style.display= "block";
this.document.all[picid].src=http://www.163design.net/p/b/"Leaf.gif";

}
Else
{
This.document.all[expid].style.display= "None";
this.document.all[picid].src=http://www.163design.net/p/b/"Parent.gif";
}
}
</script>

<body bgcolor= "#FFFFFF" text= "#000000" >
?
Require ("do_mysql.php");
$Tree = new Db_sql;
$Tree->connect ()//Connect to the database, can be changed to their own code

$SQL = "Select Departmentid,departmentname from test where length (DepartmentID) =3";//propose topmost node (ancestor node), modify 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>
&LT;TD valign= "Top" >
<div align= "center" >
<table border= "0" cellpadding= "0" cellspacing= "0" width= "372" >
<tr>
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&LT;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;//compute the number of layers, but this is a bit superfluous because it must be the first layer
$SearchPattern = $FirstDepartment. "    ___"; Set search criteria
Listchildtree ($SearchPattern, $BaseNum); Show children node of ancestor node
?>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</center>
</div>

</body>

Design of <?/* table structure

Because the test table is designed to be very simple:

CREATE TABLE Test (
ID Mediumint (8) unsigned not NULL auto_increment, #流水号
DepartmentID varchar (MB) Not NULL default ', #单位代号
Departmentname varchar (MB) Not NULL default ', #单位名称
KEY ID (ID)
)

Data insertion code I'm not going to come out here. (Easy to write, I believe you can write it)

The rules for the data table are:

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

I only set up a "ancestor" (001), so in the program directly called, can be set up according to their own, and the code to make a simple modification can be!

Well, here it is, if you have any questions welcome to discuss with me! Best wishes to all of you today happy work!
Take a smoke first and sleep! Tired! (because just wrote a webftp, if any brothers and sisters need to please 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.