21,886
社区成员
发帖
与我相关
我的任务
分享
$cats = array(
array(
'id' => 1,
'name' => '学术和教育',
'children' => array(
array(
'id' => 2,
'name' => '自然科学',
'children' => null,
),
// ...
),
),
// ...
);
$cats = array(
array(
'id' => 1,
'name' => '学术和教育',
'children' => array(
array(
'id' => 2,
'name' => '自然科学',
'children' => null,
),
),
),
array(
'id' => 3,
'name' => '农业',
'children' => array(
array(
'id' => 4,
'name' => '种植',
'children' => null,
),
array(
'id' => 5,
'name' => '养殖',
'children' => array(
array(
'id' => 6,
'name' => '养牛',
'children' => null,
),
array(
'id' => 6,
'name' => '养猪',
'children' => null,
),
),
),
),
),
);
function array2tree($array)
{
echo '<ul class="cats">',"\r\n";
foreach($array as $k=>$v)
{
$i=1;
echo '<li><a>',$v['name'],'</a>',"\r\n";
if(count($v['children'])){
echo '<a href="javascript:;" onMouseDown="var c=document.getElementById(\'catid_',$v['id'],'\');if(c.style.display==\'none\'){c.style.display=\'\';this.innerHTML=\'关闭\';}else{c.style.display=\'none\';this.innerHTML=\'展开\';}">关闭</a>';
echo '<div id="catid_',$v['id'],'">';
array2tree($v['children']);
echo '</div>';
}
echo '</li>',"\r\n";
}
echo '</ul>',"\r\n";
}
array2tree($cats);
无聊玩玩
<?php
$cats = array(
array(
'id' => 1,
'name' => '学术和教育',
'children' => array(
array(
'id' => 2,
'name' => '自然科学',
'children' => array(
array(
'id' => 3,
'name' => '生物',
'children' => null
)
)
),
array(
'id' => 4,
'name' => '人文社科',
'children' => null
)
)
),
array(
'id' => 5,
'name' => '生活',
'children' => null
)
);
class treemenue{
private $tree=array();
private $treeinfo=array();
public function __construct($arr){
$this->tree=$arr;
}
private function gettree($arr,$position){
foreach($arr as $key=>$value){
$tmp=$position;
$position.=$key;//记录当前的节点位置
$this->treeinfo["$position"]=array('id'=>$value['id'],'name'=>$value['name']);
if($value['children']!=null){
$this->gettree($value['children'],$position);
}
$position=$tmp;
}
}
public function creattree(){
$this->gettree($this->tree,null);
return $this->treeinfo;
}
public function countitem(){
return count($this->treeinfo);
}
public function getdepth(){
$maxdepth=0;
foreach($this->treeinfo as $key=>$value){
$maxdepth=$maxdepth>strlen($key)?$maxdepth:strlen($key);
}
return $maxdepth;
}
}
$t=new treemenue($cats);
$trinfo=$t->creattree();
$max=$t->getdepth();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>树形菜单</title>
<script>
var a=new Array();
var n=<?php echo $t->countitem(); ?>;
<?php
$j=0;
foreach($trinfo as $key=>$value){
?>
a[<?php echo $j;?>]="<?php echo $key; ?>"+"-";
<?php
$j++;
}
?>
function rollmenue(id){//伸缩菜单的函数
for(var i=0;i<n;i++){
if(a[i].substr(0,a[i].length-1)==id){
if(a[i].substr(a[i].length-1,1)=="+")
a[i]=id+"-";
else
a[i]=id+"+";
break;
}
}
var clmenue="";
for(var q=0;q<n;q++){
if(a[q].substr(a[q].length-1,1)=="-"){//展开的菜单
for(var k=q+1;k<n;k++){
if(a[k].length>a[q].length)
document.getElementById(a[k].substr(0,a[k].length-1)).style.display="inline-table";
else
break;
}
}
else{//缩起的菜单
for(var k=q+1;k<n;k++){
if(a[k].length>a[q].length)
document.getElementById(a[k].substr(0,a[k].length-1)).style.display="none";
else{
q=k-1;
break;
}
}
}
}
}
</script>
<style type="text/css">
#tree{
float:left;
}
<?php
for($i=0;$i<=$max;$i++){
$pos=20*$i;
$color=220*$i;
echo ".n".$i."{margin-left:{$pos}px;}";
echo ".n".$i." a{text-decoration:none;color:#ff8000;}";
echo ".m".$i."{" .
"font-weight:bold;" .
"background-color:#".$color.";".
"}";
}
?>
</style>
</head>
<body>
<div id="tree">
<?php foreach($trinfo as $key=>$value){ ?>
<div class="<?php echo "m".strlen($key);?>" id="<?php echo $key;?>" onclick="rollmenue('<?php echo $key;?>');">
<div class="<?php echo "n".strlen($key);?>">
<a href="#?id=<?php echo $value['id']; ?>"><?php echo $value['name'];?> </a>
</div>
</div>
<?php } ?>
</div>
</body>
</html>
<?php
$cats = array(
array(
'id' => 1,
'name' => '学术 && 教育',
'children' => array(
array(
'id' => 11,
'name' => '自然科学',
'children' => null
),
array(
'id' => 12,
'name' => '人文科学',
'children' => null
),
array(
'id' => 13,
'name' => '期刊会议',
'children' => null
),
array(
'id' => 14,
'name' => '高校名称',
'children' => array(
array(
'id' => 11,
'name' => '西电',
'children' => null
),
array(
'id' => 12,
'name' => '交大',
'children' => null
),
array(
'id' => 13,
'name' => '西工大',
'children' => null
),
)
)
)
),
array(
'id' => 2,
'name' => '生活',
'children' => array(
array(
'id' => 21,
'name' => '品茶',
'children' => null
),
array(
'id' => 22,
'name' => '美食',
'children' => null
),
array(
'id' => 23,
'name' => '运动',
'children' => null
),
array(
'id' => 24,
'name' => '养生',
'children' => null
)
),
)
);
function deep_array($arr){
foreach($arr as $v){
//输出一级目录
echo "<li>".$v['name']."</li>";
if(is_array($v['children'])){
//输出自己目录
echo "<ul class='wrap'>";
deep_array($v['children']);
echo "</ul>";
}
}
}
echo "<ul class='tree_root'>";
deep_array($cats);
echo "</ul>";
?>
<?php
$cats = array(
array(
'id' => 1,
'name' => '学术',
'children' => array(
array(
'id' => 8,
'name' => '学术A',
'children' => array(
array(
'id' => 9,
'name' => '学术B',
'children' => null,
), array(
'id' => 10,
'name' => '学术C',
'children' => null,
),
array(
'id' => 11,
'name' => '学术D',
'children' => array(
array(
'id' => 12,
'name' => '学术D1',
'children' => null,
),
),
),
// ...
),
),
// ...
),
),
array(
'id' => 2,
'name' => '教育',
'children' =>array(
array(
'id' => 13,
'name' => 'A教育',
'children' =>array(
array(
'id' => 14,
'name' => 'B教育',
'children' =>null,
),
),
),
array(
'id' => 15,
'name' => 'C教育',
'children' =>null,
),
),
),
array(
'id' => 3,
'name' => '生活',
'children' =>null,
),
array(
'id' => 4,
'name' => '运动.休闲',
'children' =>null,
),
array(
'id' => 5,
'name' => '大众文化',
'children' =>null,
),
array(
'id' => 6,
'name' => '娱乐',
'children' =>null,
),
array(
'id' => 7,
'name' => '城市.区划',
'children' =>null,
),
// ...
);
function showtree($cats)
{
$html = '';
$arrayCast = array();
$rows = count($cats);
for($i = 0; $i < $rows; $i++)
{
$id = $cats[$i]['id'];
$name = $cats[$i]['name'];
$arrayCast = $cats[$i]['children'];
$html .= '<li>';
if ($arrayCast != null)
{
$html .= "<a href='javascript:void(0)' onclick='retractable(".$id.")'><span id='span".$id."'>+</span>".$name."</a>";
}
else
{
$html .= ''.$name.'';
}
$html .= '</li>' ;
if ($arrayCast != null)
{
$html .= '<ul id="ul'.$id.'" style="display:none">';
$html .= showTree($arrayCast);
$html .= '</ul>';
}
}
return '<ul>'.$html.'</ul>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>baidu test</title>
<style type="text/css">
ul {margin:0px; width:100px; list-style:none;padding-left:30px;}
ul li{width:100px;;}
ul li a{text-decoration: none; }
ul li a:hover{ color:#F00; text-decoration: none;}
</style>
<script type="text/javascript" language="javascript">
/*
@Retractable 展开或收起li标签
@parm ulid : obj ul标签id
@parm signid:string +-符号id
@author zhaolei
*/
function retractable(signid)
{
var spansign = document.getElementById('span' + signid);
var ulid = document.getElementById('ul' + signid);
if (spansign.innerHTML == '-')
{
spansign.innerHTML = '+';
ulid.style.display = 'none';
}
else
{
spansign.innerHTML = '-';
ulid.style.display = 'block';
}
}
</script>
</head>
<body>
<div>
<?php echo showtree($cats);?>
</div>
</body>
</html>
<script src='jquery-latest.pack.js' language='javascript'></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
$("Table tr td Table").hide();
$("Table tr td div").click(function() {
$(this).next().toggle();
});
})
//-->
</script>
$cats = array(
array(
'id' => 1,
'name' => '学术和教育',
'children' => array(
array(
'id' => 2,
'name' => '自然科学',
'children' => null,
),
array(
'id' => 3,
'name' => '自然科学2',
'children' => array(
array(
'id' => 2,
'name' => '自然科学',
'children' => null,
),
array(
'id' => 2,
'name' => '自然科学',
'children' => null,
),
),
),
),
),
array(
'id' => 1,
'name' => '学术和教育',
'children' => array(
array(
'id' => 2,
'name' => '自然科学',
'children' => null,
),
array(
'id' => 3,
'name' => '自然科学2',
'children' => null,
),
),
),
);
function printTable($arr) {
$table = "<table style='background:#CCFFFF;border:1px solid #99FFFF;'>\r\n";
foreach($arr as $key=>$_arr) {
$childHtml = ($_arr['children'] != null) ? printTable($_arr['children']) : '';
$table .= "<tr><td id=".$_arr['id']."><div>".$_arr['name']."</div>".$childHtml."</td></tr>\r\n";
}
$table .= "</table>\r\n";
return $table;
}
echo printTable($cats)