查询所有子节点,包含节点本身(遍历树形菜单)

来源:互联网 发布:ue boom 2软件 编辑:程序博客网 时间:2024/05/19 20:59

<?php

class Wms_Tree
{
    var $table='ot_departments';/**set default table name**/

function get_AllCID($cid)
{
    $depts = array();
    $depts = $this->findAllData();
    $result=array();
    $this->findsub($depts,$cid,$result);
    return $result;
}

function findAllData(){
    global $db;
    $tree = array();
    $rs = $db->query("select * from ".$this->table." where 1 ");
    while($data = $db->fetchNextObject($rs)){
        $tree[] = array('id'=>$data->id,'pid'=>$data->pid,'name'=>$data->name,'memo'=>$data->memo);
    }
    return $tree;
}

function findsub($a,$ids,&$result){
   if(!is_array($ids))  $ids=array($ids);
   foreach ($ids as $d){
     $result[] = $d;
   }
 
   $dds =array();
   foreach ($ids as $d){
    foreach($a as $aa){
        if($aa['pid']==$d){
            $dds[] = $aa['id'];
        }
    }
   }
   if(empty($dds)) return $result;
   else $this->findsub($a,$dds,$result);

}

}

?>



使用的时候只要先将这个类实例化,再使用就可以了

require_once('class/wms.tree.php');
$tree = new Wms_Tree();
$cids = $tree->get_AllCID($_GET['dept']);


原创粉丝点击