明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

無(wú)限級(jí)別菜單的完成

[摘要]<? /* 看到很多朋友問(wèn)過(guò)無(wú)限級(jí)別菜單的的問(wèn)題(其實(shí)理論上還是有級(jí)別的,畢竟要受到個(gè)方便的條件的限制,比如: 數(shù)據(jù)庫(kù)字段的類型等),我曾經(jīng)用老大(嘮叨)提供的代碼寫出來(lái)過(guò)無(wú)限級(jí)別的菜單,但是感覺效果不是很好(視覺上),于是趁著"夜深人靜"就寫這個(gè)"無(wú)限制級(jí)別...
<?  /* 看到很多朋友問(wèn)過(guò)無(wú)限級(jí)別菜單的的問(wèn)題(其實(shí)理論上還是有級(jí)別的,畢竟要受到個(gè)方便的條件的限制,比如: 數(shù)據(jù)庫(kù)字段的類型等),我曾經(jīng)用老大(嘮叨)提供的代碼寫出來(lái)過(guò)無(wú)限級(jí)別的菜單,但是感覺效果不是很好(視覺上),于是趁著"夜深人靜"就寫這個(gè)"無(wú)限制級(jí)別的菜單",其實(shí)道理很簡(jiǎn)單,主要是數(shù)據(jù)表的設(shè)計(jì),還有遞歸方法的使用(如果有時(shí)間我會(huì)用中值排序法來(lái)做),我會(huì)在下面給出數(shù)據(jù)結(jié)構(gòu)的設(shè)計(jì)(非常簡(jiǎn)單),這里我沒有加上豎直的虛線(windows資源管理器的虛線),同時(shí)Sql語(yǔ)句我也將其固定,大家可以根據(jù)自己的需要來(lái)修改!如果有問(wèn)題可以聯(lián)系我:msn:banneryue@sina.com,QQ:7665656,E_mail:yuepengfei@mail.banner.com.cn

明天(已經(jīng)是今天了,呵呵)我會(huì)提供一個(gè)測(cè)試頁(yè)面讓大家來(lái)看(因?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ù)庫(kù)的句柄為全局
               $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=http://www.okasp.com/techinfo/"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=http://www.okasp.com/techinfo/"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ù)本身來(lái)顯示其他孩子節(jié)點(diǎn)
            }//end if?>
             </table>
            <?}//end while
           }//end function?>
<html>
<head>
<title>無(wú)限級(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=http://www.okasp.com/techinfo/"leaf.gif";

  }
  else
  {
    this.document.all[expid].style.display="none";
    this.document.all[picid].src=http://www.okasp.com/techinfo/"parent.gif";
  }
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<?
  require("do_mySql.php");
$Tree = new DB_Sql;
$Tree->connect();//連接數(shù)據(jù)庫(kù),可根據(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=http://www.okasp.com/techinfo/"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ì)得非常的簡(jiǎn)單:

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ù)插入的代碼我在這里就不那出來(lái)給大家了(很容易寫,相信大家都能寫出來(lái))

數(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ù)需要自己來(lái)設(shè)置,并對(duì)代碼作簡(jiǎn)單的修改即可!

好了,就到這里了,如果大家有問(wèn)題歡迎和我探討!最好祝大家今天工作愉快!
先吸顆煙在睡覺!好累。ㄒ?yàn)閯倓倢懥艘粋(gè)webFtp,如果哪位兄弟姐妹需要請(qǐng)mail我)
*/


?>