国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

無限級別菜單的實(shí)現(xiàn)

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

明天(已經(jīng)是今天了,呵呵)我會提供一個(gè)測試頁面讓大家來看(因?yàn)槲以谒奚嶂荒軗芴柹暇W(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被這里自動替換成"  "了
                   $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://edu.cnzz.cn/newsinfo/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?>" cellspacing="0" cellpadding="0">
                
                 <?}else{           //找到則顯示"+"圖片            
                  ?>
                   <tr><td><?=$space?><a ><span align="absmiddle"><img id="<?=$tablepic?>" src="http://edu.cnzz.cn/newsinfo/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?>" cellspacing="0" cellpadding="0">
            <?
              listchildtree($childdepartment,$basenum);//遞歸調(diào)用函數(shù)本身來顯示其他孩子節(jié)點(diǎn)
            }//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="http://edu.cnzz.cn/newsinfo/leaf.gif";

  }
  else
  {
    this.document.all[expid].style.display="none";
    this.document.all[picid].src="http://edu.cnzz.cn/newsinfo/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 ><span align="absmiddle"> <img id="dwimg" src="http://edu.cnzz.cn/newsinfo/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" 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ì)

由于是測試表設(shè)計(jì)得非常的簡單:

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個(gè)不夠,請自行添加)
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è)置,并對代碼作簡單的修改即可!

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


?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 尼木县| 都安| 墨江| 庄浪县| 广河县| 罗田县| 胶南市| 威海市| 巴彦县| 汉寿县| 江北区| 岳普湖县| 婺源县| 林甸县| 嵊泗县| 新和县| 崇阳县| 顺平县| 兴隆县| 阿拉善右旗| 宁乡县| 玉溪市| 延庆县| 丰县| 遂宁市| 巍山| 绥德县| 康马县| 文山县| 炎陵县| 扬中市| 凌源市| 平乡县| 淮南市| 屯昌县| 五原县| 哈尔滨市| 疏勒县| 毕节市| 嘉峪关市| 长顺县|