|
明天(已經(jīng)是今天了,呵呵)我會提供一個測試頁面讓大家來看(因為我在宿舍只能撥號上網(wǎng),Ip地址不固定)
*/
/** 遞歸顯示子節(jié)點函數(shù)
*
*
* @param $SearchPattern 查找的條件(like)
* @param $BaseNum 節(jié)點的層數(shù)
*/
function ListChildTree($SearchPattern,$BaseNum){
global $Tree;//聲明連接數(shù)據(jù)庫的句柄為全局
$Sql="select DepartmentId,DepartmentName from test where DepartmentId like '$SearchPattern'"; //查找孩子節(jié)點
$QueryChild=$Tree->query($Sql);
while($Result=$Tree->fetch_array($QueryChild)) { //取出孩子節(jié)點
$Space="";
for($j=0;$j<((strlen($SearchPattern)/3)-$BaseNum);$j++)
$Space.=" "; //設(shè)置顯示節(jié)點前面的距離,這里的空格的html被這里自動替換成" "了
$ChildDepartment=trim($Result[0])."___";
$ChildSql="select count(*) from test where DepartmentId like '$ChildDepartment'";//查找孩子節(jié)點的孩子節(jié)點
$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é)點的節(jié)點,則顯示"-"圖片
?>
<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é)點
}//end if?>
</table>
<?}//end while
}//end function?>
<html>
<head>
<title>無限級菜單測試</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é)點(祖宗節(jié)點),根據(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;//計算層數(shù),其實這個有點多余,因為其必為第一層
$SearchPattern=$FirstDepartment."___"; //設(shè)置查找條件
ListChildTree($SearchPattern,$BaseNum); //顯示祖宗節(jié)點的孩子節(jié)點
?>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</center>
</div>
</body>
</html>
<?/* 表結(jié)構(gòu)的設(shè)計
由于是測試表設(shè)計得非常的簡單:
CREATE TABLE test (
id mediumint(8) unsigned NOT NULL auto_increment, #流水號
DepartmentId varchar(100) NOT NULL default '', #單位代號
DepartmentName varchar(100) NOT NULL default '', #單位名稱
KEY id (id)
)
數(shù)據(jù)插入的代碼我在這里就不那出來給大家了(很容易寫,相信大家都能寫出來)
數(shù)據(jù)表的規(guī)則為:
001為第一級(如果999個不夠,請自行添加)
001001為001的第一個子節(jié)點,001002為001的第二個子節(jié)點
001001001為001001的第一個子節(jié)點,以此類推……
我這里只設(shè)置了一個"祖宗"(001),所以在程序中就直接調(diào)用了,可根據(jù)需要自己來設(shè)置,并對代碼作簡單的修改即可!
好了,就到這里了,如果大家有問題歡迎和我探討!最好祝大家今天工作愉快!
先吸顆煙在睡覺!好累!(因為剛剛寫了一個webFtp,如果哪位兄弟姐妹需要請mail我)
*/
?>
php技術(shù):無限級別菜單的實現(xiàn),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。