转载:http://blog.csdn.net/super_ufo/article/details/4554149
相关的类下载 http://d.download.csdn.net/down/1664921/super_ufo
表结构
CREATE TABLE IF NOT EXISTS `treenode` (
`node_id` int(11) NOT NULL auto_increment,
`node_path` varchar(255) NOT NULL default '',
`node_depth` int(11) NOT NULL default '0',
`node_order` int(11) NOT NULL default '0',
`node_title` varchar(255) NOT NULL default '',
`node_type` tinyint(4) NOT NULL default '0',
`node_has_childs` tinyint(4) NOT NULL default '0',
PRIMARY KEY (`node_id`),
KEY `node_id` (`node_id`),
KEY `node_path` (`node_path`),
KEY `node_depth` (`node_depth`),
KEY `node_order` (`node_order`),
KEY `node_title` (`node_title`),
KEY `node_has_childs` (`node_has_childs`)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS `team` (
`team_id` int(11) NOT NULL ,
`team_name` varchar(255) NOT NULL default '',
`team_des` int(11) NOT NULL default '0',
PRIMARY KEY (`team_id`),
KEY `team_name` (`team_name`)
) TYPE=MyISAM;
CREATE TABLE IF NOT EXISTS `contacts` (
`contacts_id` int(11) NOT NULL ,
`contacts_name` varchar(255) NOT NULL default '',
`contacts_des` varchar(255) NOT NULL default '',
`contacts_company` varchar(255) NOT NULL default '',
`contacts_address` varchar(255) NOT NULL default '',
`contacts_profession` varchar(255) NOT NULL default '',
`contacts_offphone` int(16) NULL ,
`contacts_mobile` int(12) NULL ,
`contacts_homephone` int(16) NULL ,
`contacts_age` int(3) NULL ,
`contacts_sex` varchar(2) NOT NULL default '' ,
`contacts_fax` int(16) NULL ,
`contacts_group` varchar(255) NOT NULL default '',
PRIMARY KEY (`contacts_id`),
KEY `contacts_name` (`contacts_name`),
KEY `contacts_group` (`contacts_group`),
KEY `contacts_company` (`contacts_company`),
KEY `contacts_profession` (`contacts_profession`)
) TYPE=MyISAM;
$sqlmenu = "select * from ( (select node_id ,node_path ,node_depth ,node_order ,node_title ,node_type,node_has_childs, b.parent as parent from treenode as a ,team as b where a.node_id=b.team_id order by a.node_path asc) union (select node_id ,node_path ,node_depth ,node_order ,node_title ,node_type,node_has_childs,c.contacts_group as parent from treenode as a ,contacts as c where a.node_id=c.contacts_id order by a.node_path asc) ) as d order by d.node_depth,node_id asc ";
本语句查询结果:
+---------+-----------+------------+------------+------------+-----------+-----------------+--------+
| node_id | node_path | node_depth | node_order | node_title | node_type | node_has_childs | parent |
+---------+-----------+------------+------------+------------+-----------+-----------------+--------+
| 1 | x | 1 | 0 | 开发部 | 0 | 16 | 0 |
| 2 | x | 1 | 1 | 销售部 | 0 | 0 | 0 |
| 3 | x | 1 | 1 | 生产部 | 0 | 3 | 0 |
| 226 | x | 1 | 0 | 人事部 | 0 | 1 | 0 |
| 5 | x.3 | 2 | 2 | 生产部2 | 0 | 11 | 3 |
| 6 | x.3 | 2 | 3 | 生产部3 | 0 | 3 | 3 |
| 190 | x.3 | 2 | 0 | liuniuyou | 1 | 0 | 3 |
| 195 | x.1 | 2 | 0 | 开发部0 | 0 | 1 | 1 |
| 220 | x.3 | 2 | 0 | hello | 1 | 0 | 3 |
| 227 | x.226 | 2 | 0 | kugoo | 1 | 0 | 226 |
| 193 | x.3.5 | 3 | 0 | 22 | 1 | 0 | 5 |
| 228 | x.3.6 | 3 | 0 | 22 | 1 | 0 | 6 |
| 229 | x.1.195 | 3 | 0 | 1 | 1 | 0 | 195 |
<?php
/************************************************************************************
*加载菜单
************************************************************************************/
$sqlmenu = "select * from ( (select node_id ,node_path ,node_depth ,node_order ,node_title ,node_type,node_has_childs, b.parent as parent from treenode as a ,team as b where a.node_id=b.team_id order by a.node_path asc) union (select node_id ,node_path ,node_depth ,node_order ,node_title ,node_type,node_has_childs,c.contacts_group as parent from treenode as a ,contacts as c where a.node_id=c.contacts_id order by a.node_path asc) ) as d order by d.node_depth,node_id asc ";
$nodes = $pDB->FetchTable($sqlmenu,true);
foreach ($nodes as $key=>$value){
if($value['parent']==0)
$menu_node['x']["$value[node_id]"] = array('title' => $value['node_title'],'type'=>$value['node_type']);
else{
$level = explode('.',$value['node_path']);
$key_str= '$menu_node[/'x/']';
for($i=1; $i<count($level); $i++){
$key_str.= "['".$level[$i]."']";
}
$key_str.= "['".$value['node_id']."']";
$eval_str= $key_str. "['title'] ='".$value['node_title']."';". $key_str. "['type'] ='".$value['node_type']."';";
eval ($eval_str);
}
}
$menu = creat_htmlul_tree($menu_node['x']);
$smarty->assign("menu", $menu);
#递归得到树形菜单结构
function creat_htmlul_tree($aNodes) {
static $sTreeContent = "<ul id='browser2' class='filetree treeview-famfamfam'>";
#echo "<pre>"; ;print_r($aNodes); echo "</pre>";
foreach( $aNodes as $k=>$v ) {
if(count($v) > 2 && $k!='title' && $k!='type') {
if($v['type']==0)
$sTreeContent .= "<li><span class='folder'><a href='?menu=phonecontact&nodetype=".$v['type']."&prekey=".$k."'>".$v['title']."</a></span><ul>";
else
$sTreeContent .= "<li><span class='file'><a href='?menu=phonecontact&nodetype=".$v['type']."&prekey=".$k."'>".$v['title']."</a></span><ul>";
creat_htmlul_tree($v);
$sTreeContent .= '</li>';
} else {
if($k!='title' && $k!='type'){
if($v['type']==0)
$sTreeContent .= "<li><span class='folder'><a href='?menu=phonecontact&nodetype=".$v['type']."&prekey=".$k."'>".$v['title']."</a></span></li>";
else
$sTreeContent .= "<li><span class='file'><a href='?menu=phonecontact&nodetype=".$v['type']."&prekey=".$k."'>".$v['title']."</a></span></li>";
}
}
}
$sTreeContent.="</ul>";
#echo htmlentities($sTreeContent);
return $sTreeContent;
}
?>
tpl 页面
<link rel="stylesheet" href="./modules/phonecontact/jquery-treeview/jquery.treeview.css" type="text/css" />
<link rel="stylesheet" href="./modules/phonecontact/jquery-treeview/screen.css" type="text/css" />
<script src="./modules/phonecontact/jquery-treeview/lib/jquery.js" type="text/javascript" language="JavaScript"></script>
<script src="./modules/phonecontact/jquery-treeview/lib/jquery.cookie.js" type="text/javascript" language="JavaScript"></script>
<script src="./modules/phonecontact/jquery-treeview/jquery.treeview.js" type="text/javascript" language="JavaScript"></script>
{literal}
<script type="text/javascript">
$(document).ready(function(){
$("#browser1").treeview({
collapsed: false,
unique: true,
persist: "cookie",
cookieId: "navigationtree1"
});
$("#browser2").treeview({
persist: "cookie",
collapsed: true,
unique: false,
cookieId: "navigationtree2"
});
$("#browser3").treeview({
persist: "location",
collapsed: true,
unique: true
});
$("#browser4").treeview({
persist: "location",
collapsed: true,
unique: true
});
$("#browser5").treeview({
animated: "slow",
persist: "location",
collapsed: true,//是否都折叠
unique: true//同一层次只允许展开一个
});
});
</script>
{/literal}
<table width="99%" border="0" cellspacing="0" cellpadding="0" align="top" valign="top" style="" >
<tr align="top" valign="top" >
<td width="20%" height="100%" bgcolor="#f6f6f6" class="menuiz_botonoff">
<div style = "align:left" height = "100%" >
{$menu}
</div>
</td>