PHP无限分类的类


Posted in PHP onJanuary 02, 2007
<?php 
/** 
 * @author        YangHuan 
 * @datetime     
 * @version        1.0.0 
 */ 
/** 
 * 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代码:

<?php
//new Tree(根目录的名字); 
//根目录的ID自动分配为0 
$Tree = new Tree('根目录'); 
//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)."<br>\n"; 
}

PHP无限分类-PHP100代码

<?php 
//无限分类,从子类找所有父类
//$id 子类ID
 function php100_xd($id){
   $sql="select * from fl where id='$id'";
   $q=mysql_query($sql);
   $rs=mysql_fetch_array($q);
   $rs['fid']==0 ? "" : fl($rs['fid']);
   echo $rs['name']."-";
   }//读取所有父类下面的子类
//$f顶级分类从什么开始,$s样式
 function php100_dx($f=0,$s=""){
   $sql="select * from fl where fid=$f";
   $q=mysql_query($sql);
   $s=$s."-";
   while($rs=mysql_fetch_array($q)){
     echo "<br>$s".$rs['name'];
  flt($rs['id'],$s);
     } 
   }
PHP 相关文章推荐
Discuz 模板语句分析及知识技巧
Aug 21 PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 PHP
ThinkPHP关于session的操作方法汇总
Jul 18 PHP
php+jQuery.uploadify实现文件上传教程
Dec 26 PHP
php循环table实现一行两列显示的方法
Jun 04 PHP
Zend Framework教程之Zend_Registry对象用法分析
Mar 22 PHP
php数组分页实现方法
Apr 30 PHP
PHP下载文件的函数实例代码
May 18 PHP
thinkPHP5框架auth权限控制类与用法示例
Jun 12 PHP
PHP实现一个轻量级容器的方法
Jan 28 PHP
关于laravel 日志写入失败问题汇总
Oct 17 PHP
Laravel手动返回错误码示例
Oct 22 PHP
php你的验证码安全码?
Jan 02 #PHP
一个PHP操作Access类(PHP+ODBC+Access)
Jan 02 #PHP
一个用php实现的获取URL信息的类
Jan 02 #PHP
PHP 和 MySQL 开发的 8 个技巧
Jan 02 #PHP
Smarty结合Ajax实现无刷新留言本实例
Jan 02 #PHP
Ajax PHP分页演示
Jan 02 #PHP
windows下PHP APACHE MYSQ完整配置
Jan 02 #PHP
You might like
php缓冲 output_buffering和ob_start使用介绍
2014/01/30 PHP
Yii2中OAuth扩展及QQ互联登录实现方法
2016/05/16 PHP
详谈PHP中的密码安全性Password Hashing
2017/02/04 PHP
又一个图片自动缩小的JS代码
2007/03/10 Javascript
基于jquery的划词搜索实现(备忘)
2010/09/14 Javascript
用jquery实现动画跳到顶部和底部(这个比较简单)
2014/09/01 Javascript
jQuery中serializeArray()与serialize()的区别实例分析
2015/12/09 Javascript
基于Bootstrap的UI扩展 StyleBootstrap
2016/06/17 Javascript
js轮盘抽奖实例分析
2020/04/17 Javascript
webpack实用小功能介绍
2018/01/02 Javascript
vue-cli结合Element-ui基于cropper.js封装vue实现图片裁剪组件功能
2018/03/01 Javascript
Vue+Element-U实现分页显示效果
2020/11/15 Javascript
vue+Element-ui实现登录注册表单
2020/11/17 Javascript
[01:56]生活中的妖精之七夕特别档
2016/08/09 DOTA
[01:26]神话结束了,却也刚刚开始——DOTA2新英雄玛尔斯驾临战场
2019/03/10 DOTA
使用Node.js和Socket.IO扩展Django的实时处理功能
2015/04/20 Python
详解python之简单主机批量管理工具
2017/01/27 Python
Python列表解析配合if else的方法
2018/06/23 Python
python3解析库pyquery的深入讲解
2018/06/26 Python
浅析python3字符串格式化format()函数的简单用法
2018/12/07 Python
Django发送邮件和itsdangerous模块的配合使用解析
2019/08/10 Python
python常用数据重复项处理方法
2019/11/22 Python
Python 中的pygame安装与配置教程详解
2020/02/10 Python
Pycharm配置PyQt5环境的教程
2020/04/02 Python
python 使用建议与技巧分享(四)
2020/08/18 Python
Python页面加载的等待方式总结
2021/02/28 Python
CSS3 :nth-child()伪类选择器实现奇偶行显示不同样式
2013/11/05 HTML / CSS
CSS3之transition实现下划线的示例代码
2018/05/30 HTML / CSS
碧欧泉美国官网:Biotherm美国
2016/08/31 全球购物
传播学专业毕业生自荐书
2014/07/01 职场文书
校车安全责任书
2014/08/25 职场文书
幼儿园教师节感谢信
2015/01/23 职场文书
保研推荐信范文
2015/03/25 职场文书
2015年乡镇环保工作总结
2015/04/22 职场文书
导游词之南京中山陵
2019/11/27 职场文书
python 如何做一个识别率百分百的OCR
2021/05/29 Python