foreach ($category as $key=>$value) { echo $value["id"].$value["layer"]."分类名:".$value["catname"]."父ID".$value["pid"]."级别".$value["level"]."\n"; } */ ?>
可以用递归 也可以用 在数据库存放一个 排序code的方式来排序
我自己用的,没用递归 class MenuTree { public $data = array(); public $pdata = array(); public $menuStr = null; public $indentArr = array(); public $imgPath; public $power = array(); public $layer = 1; public $root_id = 0;
function __construct(){ $this->imgPath = IMG_URL . "menu/"; }
图片就在对应的目录里下,JS文件里有图片地址。
自己在网上搜吧,还有JQUERY的那么多树插件。
数据可以是动态的。当然用JQUERY的树更好,checktree,jsTree都是比较可以的树。
$opts = array();
$prefix = str_repeat('--', $level).' ';
foreach ($list as $item){
$opts[] = '<option id="id_'.$item->get('id').'">'.$prefix.$item->get('name').'</option>';
if (isset($item->subList)){
$opts = array_merge($opts, renderCatList($item->subList, $level+1));
}
}
return $opts;
}
<?php
class Tree
{
public $data=array();
public $cateArray=array();
//public $ids=array();
function Tree()
{
}
function setNode ($id, $parent, $value)
{
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->cateArray[$id] = $parent;
// $this->ids[$id] = $id;
}
function getChildsTree($id=0)
{
$childs=array();
foreach ($this->cateArray as $child=>$parent)
{
if ($parent==$id)
{
$childs[$child]=$this->getChildsTree($child);
}
}
return $childs;
}
function getChilds($id=0)
{
$childArray=array();
$childs=$this->getChild($id);
foreach ($childs as $child)
{
$childArray[]=$child;
$childArray=array_merge($childArray,$this->getChilds($child));
}
return $childArray;
}
function getChild($id)
{
$childs=array(); //print_r( $this->cateArray );foreach ($this->cateArray as $child=>$parent)
{
if ($parent==$id)
{
$childs[$child]=$child;
}
} //print_r( $childs );return $childs;
} //单线获取父节点
function getNodeLever($id)
{
$parents=array();
if (key_exists($this->cateArray[$id],$this->cateArray))
{
$parents[]=$this->cateArray[$id];
$parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
}
return $parents;
}
function getLayer($id,$preStr='━')
{
return '┣'.str_repeat($preStr,count($this->getNodeLever($id)));
}
function getValue ($id)
{
return $this->data[$id];
} // end func function getParent($id)
{
return $this->cateArray[$id];
} // end func
function showCategory(){
// $category = $this->ids;
$category =$this->getChilds();
foreach ($category as $key=>$id)
{
//echo $id.$Tree->getLayer($id, '-').$Tree->getValue($id)."\n";
$data[$key]["id"]=$id;
$data[$key]["layer"]=$this->getLayer($id, '━');
$data[$key]["catname"]=$this->getValue($id);
$data[$key]["pid"]=$this->getParent($id);
$data[$key]["level"]=count($this->getNodeLever($id));
}
return $data;}
}
$Tree = new Tree();
//setNode(目录ID,上级ID,目录名字);
$Tree->setNode(1, 0, '目录1');
$Tree->setNode(2, 1, '目录2');
$Tree->setNode(5, 3, '目录5');
$Tree->setNode(3, 0, '目录3');
$Tree->setNode(4, 2, '目录4');
$Tree->setNode(9, 4, '目录9');
$Tree->setNode(6, 2, '目录6');
$Tree->setNode(7, 2, '目录7');
$Tree->setNode(8, 3, '目录8');
$Tree->setNode(10, 9, '目录10');
//print_r($Tree->getChildsTree(0));
//print_r($Tree->getChild(0));
//print_r($Tree->getLayer(2));
$category = $Tree->showCategory(); print_r($category);
遍历输出
foreach ($category as $key=>$value)
{
echo $value["id"].$value["layer"]."分类名:".$value["catname"]."父ID".$value["pid"]."级别".$value["level"]."\n";
}
*/ ?>
也可以用 在数据库存放一个 排序code的方式来排序
class MenuTree
{
public $data = array();
public $pdata = array();
public $menuStr = null;
public $indentArr = array();
public $imgPath;
public $power = array();
public $layer = 1;
public $root_id = 0;
function __construct(){
$this->imgPath = IMG_URL . "menu/";
}
public function setData($data)
{
if(!is_array($data)) return;
foreach($data as $a)
{
$this->data[$a['parent_id']][] = array($a['func_id'],$a['func_name'],$a['funccode'],$a['ico_img']);
}
foreach($this->data as $k=>$a)
{
$this->pdata[] = $k;
}
}
public function setCheckData($data)
{
if(!is_array($data)) return;
$this->power = $data;
}
public function create($pid){
if(is_array($this->data[$pid]))
{
if($this->root_id===0) $this->root_id = $pid;
$end = end($this->data[$pid]);
foreach($this->data[$pid] as $k=>$a)
{
if($this->root_id==$pid && $k>0) $this->layer = 0;
$this->menuStr .= "<div>";
$this->indent($a[0],$pid);
if(in_array($a[0],$this->pdata))
{
$ico = $a[3]?$a[3]:"folder.gif";
$this->menuStr .= "<img src=\"" . $this->imgPath . $ico . "\">";
}
else
{
$ico = $a[3]?$a[3]:"page.gif";
$this->menuStr .= "<img src=\"" . $this->imgPath . $ico . "\">";
}
$isend = $a[0]==$end[0]?1:0;
$this->menuStr .= "<a href=\"javascript:cNode('img" . $a[0] . "','menu" . $a[0] . "'," . $isend . ",'$a[2]');\">$a[1]</a></div>\n";
if(in_array($a[0],$this->pdata))
{
$this->menuStr .= "<div id=\"menu" . $a[0] . "\"";
if($this->layer===0) $this->menuStr .= "style=\"display:none;\"";
$this->menuStr .= ">\n";
$this->create($a[0]);
$this->menuStr .= "</div>\n";
}
array_pop($this->indentArr);
}
}
}
public function indent($id,$pid)
{
foreach($this->indentArr as $a)
{
$img= $a==0?'line.gif':'empty.gif';
$this->menuStr .= "<img src=\"" . $this->imgPath . $img . "\">";
}
$end = end($this->data[$pid]);
$isend = $end[0]==$id?1:0;
array_push($this->indentArr,$isend);
if($isend)
{
if(in_array($id,$this->pdata))
{
$img = $this->layer?'minusbottom.gif':'plusbottom.gif';
$this->menuStr .= "<img src=\"" . $this->imgPath . $img . "\" id=\"img" . $id. "\" onclick=\"cNode('img" . $id . "','menu" . $id . "',1,'');\" style=\"cursor:pointer;\">";
}
else
{
$this->menuStr .= "<img src=\"" . $this->imgPath . "joinbottom.gif\">";
}
}
else
{
if(in_array($id,$this->pdata))
{
$img = $this->layer?'minus.gif':'plus.gif';
$this->menuStr .= "<img src=\"" . $this->imgPath . $img ."\" id=\"img" . $id. "\" onclick=\"cNode('img" . $id . "','menu" . $id . "',0,'');\" style=\"cursor:pointer;\">";
}
else
{
$this->menuStr .= "<img src=\"" . $this->imgPath . "join.gif\">";
}
}
}
public function roleindent($id,$pid)
{
foreach($this->indentArr as $a)
{
$img = 'empty.gif';
$this->menuStr .= "<img src=\"" . $this->imgPath . $img . "\">";
}
$end = end($this->data[$pid]);
$isend = $end[0]==$id?1:0;
if($pid!==0) array_push($this->indentArr,$isend);
}
}