可以生成任何树型结构
分类:热门新闻

复制代码 代码如下:

复制代码 代码如下:

/**
* 通用的树型类,可以改动任何树型结构
*/
class tree
{
    /**
    * 生成树型结构所须求的2维数组
    * @var array
    */
    var $arr = array();

array('id'=>'1','parentid'=>0,'name'=>'超级栏目少年老成'卡塔尔国, * 2 => array('id'=>'2','parentid'=>0,'name'=>'一流栏目二'卡塔尔, * 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目风流倜傥'卡塔尔, * 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'卡塔尔(قطر‎, * 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'卡塔尔国, * 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目大器晚成'State of Qatar, * 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二'卡塔尔国 * ) */ function tree { $this->arr = $arr; $this->ret = ''; return is_array; } /** * 拿到父级数组 * @param int * @return array */ function get_parent { $newarr = array(); if(!isset return false; $pid = $this->arr[$myid]['parentid']; $pid = $this->arr[$pid]['parentid']; if { foreach($this->arr as $id => $a) { if($a['parentid'] == $pid) $newarr[$id] = $a; } } return $newarr; } /** * 获得子级数组 * @param int * @return array */ function get_child { $a = $newarr = array(); if { foreach($this->arr as $id => $a) { if($a['parentid'] == $myid) $newarr[$id] = $a; } } return $newarr ? $newarr : false; } /** * 得到当前地点数组 * @param int * @return array */ function get_pos { $a = array(); if(!isset return false; $newarr[] = $this->arr[$myid]; $pid = $this->arr[$myid]['parentid']; if(isset { $this->get_pos; } if { krsort; foreach { $a[$v['id']] = $v; } } return $a; } /** * ------------------------------------- * 获得树型构造 * ------------------------------------- * @author Midnight, yangyunzhou@foxmail.com * @param $myid 表示收获这些ID下的富有子级 * @param $str 生成树形布局基本代码, 例如: "$spacer$name" * @param $sid 被选中的ID, 比方在做树形下拉框的时候供给用到 * @param $adds * @param $str_group * @return unknown_type */ function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '') { $number=1; $child = $this->get_child; if { $total = count; foreach { $j=$k=''; if { $j .= $this->icon[2]; } else { $j .= $this->icon[1]; $k = $adds ? $this->icon[0] : ''; } $spacer = $adds ? $adds.$j : ''; $selected = $id==$sid ? 'selected' : ''; @extract; $parentid == 0 && $str_group ? eval("$nstr = "$str_group";") : eval; $this->ret .= $nstr; $this->get_tree($id, $str, $sid, $adds.$k.'',$str_group); $number++; } } return $this->ret; } /** * 同上黄金年代措施相符,但允许多选 */ function get_tree_multi($myid, $str, $sid = 0, $adds = '') { $number=1; $child = $this->get_child; if { $total = count; foreach { $j=$k=''; if { $j .= $this->icon[2]; } else { $j .= $this->icon[1]; $k = $adds ? $this->icon[0] : ''; } $spacer = $adds ? $adds.$j : ''; $selected = $this->have ? 'selected' : ''; //echo $sid.'=>'.$id.' : '.$selected.' .
'; @extract; eval; $this->ret .= $nstr; $this->get_tree_multi($id, $str, $sid, $adds.$k.''); $number++; } } return $this->ret; } function have{ return(strpos(',,'.$list.',',','.$item.',')); }}?>

    /**
    * 生成树型构造所需修饰符号,能够换来图片
    * @var array
    */
    var $icon = array('│','├','└');

    /**
    * @access private
    */
    var $ret = '';

    /**
    * 结构函数,开端化类
    * @param array 2维数组,例如:
    * array(
    *      1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'卡塔尔(قطر‎,
    *      2 => array('id'=>'2','parentid'=>0,'name'=>'顶尖栏目二'卡塔尔(قطر‎,
    *      3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目黄金年代'卡塔尔,
    *      4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'卡塔尔,
    *      5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'卡塔尔,
    *      6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目少年老成'卡塔尔国,
    *      7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二'卡塔尔国
    *      )
    */
    function tree($arr=array())
    {
       $this->arr = $arr;
       $this->ret = '';
       return is_array($arr);
    }

    /**
    * 获得父级数组
    * @param int
    * @return array
    */
    function get_parent($myid)
    {
        $newarr = array();
        if(!isset($this->arr[$myid])) return false;
        $pid = $this->arr[$myid]['parentid'];
        $pid = $this->arr[$pid]['parentid'];
        if(is_array($this->arr))
        {
            foreach($this->arr as $id => $a)
            {
                if($a['parentid'] == $pid) $newarr[$id] = $a;
            }
        }
        return $newarr;
    }

    /**
    * 获得子级数组
    * @param int
    * @return array
    */
    function get_child($myid)
    {
        $a = $newarr = array();
        if(is_array($this->arr))
        {
            foreach($this->arr as $id => $a)
            {
                if($a['parentid'] == $myid) $newarr[$id] = $a;
            }
        }
        return $newarr ? $newarr : false;
    }

    /**
    * 获得当前地方数组
    * @param int
    * @return array
    */
    function get_pos($myid,&$newarr)
    {
        $a = array();
        if(!isset($this->arr[$myid])) return false;
        $newarr[] = $this->arr[$myid];
        $pid = $this->arr[$myid]['parentid'];
        if(isset($this->arr[$pid]))
        {
            $this->get_pos($pid,$newarr);
        }
        if(is_array($newarr))
        {
            krsort($newarr);
            foreach($newarr as $v)
            {
                $a[$v['id']] = $v;
            }
        }
        return $a;
    }

 
    /**
     * -------------------------------------
     *  拿到树型布局
     * -------------------------------------
     * @author  Midnight(杨云洲),  yangyunzhou@foxmail.com
     * @param $myid 表示收获那些ID下的全体子级
     * @param $str 生成树形布局基本代码, 譬喻: "$spacer$name"
     * @param $sid 被选中的ID, 比方在做树形下拉框的时候须要利用
     * @param $adds
     * @param $str_group
     * @return unknown_type
     */
    function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '')
    {
        $number=1;
        $child = $this->get_child($myid);
        if(is_array($child))
        {
            $total = count($child);
            foreach($child as $id=>$a)
            {
                $j=$k='';
                if($number==$total)
                {
                    $j .= $this->icon[2];
                }
                else
                {
                    $j .= $this->icon[1];
                    $k = $adds ? $this->icon[0] : '';
                }
                $spacer = $adds ? $adds.$j : '';
                $selected = $id==$sid ? 'selected' : '';
                @extract($a);
                $parentid == 0 && $str_group ? eval("$nstr = "$str_group";") : eval("$nstr = "$str";");
                $this->ret .= $nstr;
                $this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group);
                $number++;
            }
        }
        return $this->ret;
    }
    /**
    * 同上一方式近似,但允相当多选
    */
    function get_tree_multi($myid, $str, $sid = 0, $adds = '')
    {
        $number=1;
        $child = $this->get_child($myid);
        if(is_array($child))
        {
            $total = count($child);
            foreach($child as $id=>$a)
            {
                $j=$k='';
                if($number==$total)
                {
                    $j .= $this->icon[2];
                }
                else
                {
                    $j .= $this->icon[1];
                    $k = $adds ? $this->icon[0] : '';
                }
                $spacer = $adds ? $adds.$j : '';

                $selected = $this->have($sid,$id) ? 'selected' : '';
                //echo $sid.'=>'.$id.' : '.$selected.' .
';
                @extract($a);
                eval("$nstr = "$str";");
                $this->ret .= $nstr;
                $this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
                $number++;
            }
        }
        return $this->ret;
    }

    function have($list,$item){
        return(strpos(',,'.$list.',',','.$item.','));
    }
}
?>

威尼斯登录首页 1

*威尼斯登录首页,

*威尼斯登录首页 2

本文由威尼斯在线注册平台发布于热门新闻,转载请注明出处:可以生成任何树型结构

上一篇:配备文件config/expert.php 下一篇:PHP完结邮件群发的源码
猜你喜欢
热门排行
精彩图文