|
明天(已經(jīng)是今天了,呵呵)我會(huì)提供一個(gè)測(cè)試頁面讓大家來看(因?yàn)槲以谒奚嶂荒軗芴?hào)上網(wǎng),Ip地址不固定)
*/
/** 遞歸顯示子節(jié)點(diǎn)函數(shù)
*
*
* @param $SearchPattern 查找的條件(like)
* @param $BaseNum 節(jié)點(diǎn)的層數(shù)
*/
function ListChildTree($SearchPattern,$BaseNum){
global $Tree;//聲明連接數(shù)據(jù)庫的句柄為全局
$Sql="select DepartmentId,DepartmentName from test where DepartmentId like '$SearchPattern'"; //查找孩子節(jié)點(diǎn)
$QueryChild=$Tree->query($Sql);
while($Result=$Tree->fetch_array($QueryChild)) { //取出孩子節(jié)點(diǎn)
$Space="";
for($j=0;$j<((strlen($SearchPattern)/3)-$BaseNum);$j++)
$Space.=" "; //設(shè)置顯示節(jié)點(diǎn)前面的距離,這里的空格的html被這里自動(dòng)替換成" "了
$ChildDepartment=trim($Result[0])."___";
$ChildSql="select count(*) from test where DepartmentId like '$ChildDepartment'";//查找孩子節(jié)點(diǎn)的孩子節(jié)點(diǎn)
$ChildResult=$Tree->query_first($ChildSql);
$TableId="ta".trim($Result[0]); //設(shè)置表格Id
$TablePic="ta".trim($Result[0])."pic"; //設(shè)置圖片Id
if($ChildResult[0]<1){//如果沒有找到孩子節(jié)點(diǎn)的節(jié)點(diǎn),則顯示"-"圖片
?>
<tr><td><?=$Space?><span align="absmiddle"><img src="leaf.gif" border="0" align="absmiddle" width="35" height="17"></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{ //找到則顯示"+"圖片
?>
<tr><td><?=$Space?><a onclick="Javascript:expands('<?=$TableId?>','<?=$TablePic?>')" style="cursor:hand"><span align="absmiddle"><img id="<?=$TablePic?>" 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);//遞歸調(diào)用函數(shù)本身來顯示其他孩子節(jié)點(diǎn)
}//end if?>
</table>
<?}//end while
}//end function?>
<html>
<head>
<title>無限級(jí)菜單測(cè)試</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) //顯示圖片張合的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="leaf.gif";
}
else
{
this.document.all[expid].style.display="none";
this.document.all[picid].src="parent.gif";
}
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?
require("do_mySql.php");
$Tree = new DB_Sql;
$Tree->connect();//連接數(shù)據(jù)庫,可根據(jù)需要換成自己的代碼
$Sql="select DepartmentId,DepartmentName from test where length(DepartmentId)=3";//提出最上層節(jié)點(diǎn)(祖宗節(jié)點(diǎn)),根據(jù)需要自己修改
$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"> <img id="dwimg" SRC="parent.gif" border="0" align="absmiddle" width="35" height="17"></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;//計(jì)算層數(shù),其實(shí)這個(gè)有點(diǎn)多余,因?yàn)槠浔貫榈谝粚?br> $SearchPattern=$FirstDepartment."___"; //設(shè)置查找條件
ListChildTree($SearchPattern,$BaseNum); //顯示祖宗節(jié)點(diǎn)的孩子節(jié)點(diǎn)
?>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</center>
</div>
</body>
</html>
<?/* 表結(jié)構(gòu)的設(shè)計(jì)
由于是測(cè)試表設(shè)計(jì)得非常的簡單:
CREATE TABLE test (
id mediumint(8) unsigned NOT NULL auto_increment, #流水號(hào)
DepartmentId varchar(100) NOT NULL default '', #單位代號(hào)
DepartmentName varchar(100) NOT NULL default '', #單位名稱
KEY id (id)
)
數(shù)據(jù)插入的代碼我在這里就不那出來給大家了(很容易寫,相信大家都能寫出來)
數(shù)據(jù)表的規(guī)則為:
001為第一級(jí)(如果999個(gè)不夠,請(qǐng)自行添加)
001001為001的第一個(gè)子節(jié)點(diǎn),001002為001的第二個(gè)子節(jié)點(diǎn)
001001001為001001的第一個(gè)子節(jié)點(diǎn),以此類推……
我這里只設(shè)置了一個(gè)"祖宗"(001),所以在程序中就直接調(diào)用了,可根據(jù)需要自己來設(shè)置,并對(duì)代碼作簡單的修改即可!
好了,就到這里了,如果大家有問題歡迎和我探討!最好祝大家今天工作愉快!
先吸顆煙在睡覺!好累!(因?yàn)閯倓倢懥艘粋€(gè)webFtp,如果哪位兄弟姐妹需要請(qǐng)mail我)
*/
?>
php技術(shù):無限級(jí)別菜單的實(shí)現(xiàn),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。