找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 811|回复: 0
打印 上一主题 下一主题

PHP无限分类的类

[复制链接]

2560

主题

2560

帖子

7622

积分

论坛元老

Rank: 8Rank: 8

积分
7622
跳转到指定楼层
楼主
发表于 2018-2-14 09:25:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            [U]复制代码[/U] 代码如下:
/**
* Short description.
*
* Detail description
* @author        
* @version      1.0
* @copyright     
* @access       public
*/
class Tree
{
    /**
     * Description
     * @var        
     * @since     1.0
     * @access    private
     */
    var $data    = array();
    /**
     * Description
     * @var        
     * @since     1.0
     * @access    private
     */
    var $child    = array(-1=>array());
    /**
     * Description
     * @var        
     * @since     1.0
     * @access    private
     */
    var $layer    = array(-1=>-1);
    /**
     * Description
     * @var        
     * @since     1.0
     * @access    private
     */
    var $parent    = array();
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function Tree ($value)
    {
        $this->setNode(0, -1, $value);
    } // end func
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function setNode ($id, $parent, $value)
    {
        $parent = $parent?$parent:0;
        $this->data[$id]            = $value;
        $this->child[$id]            = array();
        $this->child[$parent][]        = $id;
        $this->parent[$id]            = $parent;
        if (!isset($this->layer[$parent]))
        {
            $this->layer[$id] = 0;
        }
        else
        {
            $this->layer[$id] = $this->layer[$parent] + 1;
        }
    } // end func
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none  
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getList (&$tree, $root= 0)
    {
        foreach ($this->child[$root] as $key=>$id)
        {
            $tree[] = $id;
            if ($this->child[$id]) $this->getList($tree, $id);
        }
    } // end func
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getValue ($id)
    {
        return $this->data[$id];
    } // end func
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getLayer ($id, $space = false)
    {
        return $space?str_repeat($space, $this->layer[$id])this->layer[$id];
    } // end func
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getParent ($id)
    {
        return $this->parent[$id];
    } // end func
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getParents ($id)
    {
        while ($this->parent[$id] != -1)
        {
            $id = $parent[$this->layer[$id]] = $this->parent[$id];
        }
        ksort($parent);
        reset($parent);
        return $parent;
    } // end func
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getChild ($id)
    {
        return $this->child[$id];
    } // end func
     
    /**
     * Short description.  
     *
     * Detail description
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getChilds ($id = 0)
    {
        $child = array($id);
        $this->getList($child, $id);
        return $child;
    } // end func
} // end class
?>
使用方法
PHP代码:
[U]复制代码[/U] 代码如下:
//setNode(目录ID,上级ID,目录名字);
$Tree->setNode(1, 0, '目录1');
$Tree->setNode(2, 0, '目录2');
$Tree->setNode(3, 0, '目录3');
$Tree->setNode(4, 3, '目录3.1');
$Tree->setNode(5, 3, '目录3.2');
$Tree->setNode(6, 3, '目录3.3');
$Tree->setNode(7, 2, '目录2.1');
$Tree->setNode(8, 2, '目录2.2');
$Tree->setNode(9, 2, '目录2.3');
$Tree->setNode(10, 6, '目录3.3.1');
$Tree->setNode(11, 6, '目录3.3.2');
$Tree->setNode(12, 6, '目录3.3.3');
//getChilds(指定目录ID);
//取得指定目录下级目录.如果没有指定目录就由根目录开始
$category = $Tree->getChilds();
//遍历输出
foreach ($category as $key=>$id)
{
    echo $Tree->getLayer($id, '|-').$Tree->getValue($id)."
\n";
}
PHP无限分类-PHP100代码
[U]复制代码[/U] 代码如下:
            
            
您可能感兴趣的文章:
  • 使用PHP数组实现无限分类,不使用数据库,不使用递归.
  • 帖几个PHP的无限分类实现想法~
  • php 无限分类的树类代码
  • php递归实现无限分类生成下拉列表的函数
  • PHP无限分类代码,支持数组格式化、直接输出菜单两种方式
  • PHP 无限分类三种方式 非函数的递归调用!
  • php无限分类且支持输出树状图的详细介绍
  • PHP无限分类(树形类)
  • php无限分类使用concat如何实现
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端