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 相关文章推荐
一个可以删除字符串中HTML标记的PHP函数
Oct 09 PHP
PHP分页显示制作详细讲解
Nov 19 PHP
php绝对路径与相对路径之间关系的的分析
Mar 03 PHP
php 获取本机外网/公网IP的代码
May 09 PHP
php 提速工具eAccelerator 配置参数详解
May 16 PHP
php各种编码集详解和以及在什么情况下进行使用
Sep 11 PHP
PHP独立Session数据库存储操作类分享
Jun 11 PHP
一个图片地址分解程序(用于PHP小偷程序)
Aug 23 PHP
分享最受欢迎的5款PHP框架
Nov 27 PHP
再Docker中架设完整的WordPress站点全攻略
Jul 29 PHP
PHP编程计算两个时间段是否有交集的实现方法(不算边界重叠)
May 30 PHP
PHP array_shift()用法实例分析
Jan 07 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
虫族 Zerg 历史背景
2020/03/14 星际争霸
了解咖啡雨林联盟认证 什么是雨林认证 雨林认证是什么意思
2021/03/05 新手入门
openPNE常用方法分享
2011/11/29 PHP
php cookie中点号(句号)自动转为下划线问题
2014/10/21 PHP
php天翼开放平台短信发送接口实现方法
2014/12/22 PHP
破除网页鼠标右键被禁用的绝招大全
2006/12/27 Javascript
Jquery显示和隐藏元素或设为只读(含Ligerui的控件禁用,实例说明介绍)
2013/07/09 Javascript
Jquery选择器中使用变量实现动态选择例子
2014/07/25 Javascript
JavaScript中用字面量创建对象介绍
2014/12/31 Javascript
Javascript基础教程之数据类型 (字符串 String)
2015/01/18 Javascript
JQuery实现超链接鼠标提示效果的方法
2015/06/10 Javascript
jQuery实现带遮罩层效果的blockUI弹出层示例【附demo源码下载】
2016/09/14 Javascript
详解如何使用babel进行es6文件的编译
2018/05/29 Javascript
vue.js单文件组件中非父子组件的传值实例
2018/09/13 Javascript
Mint UI实现A-Z字母排序的城市选择列表
2018/12/28 Javascript
微信小程序开发常见问题及解决方案
2019/07/11 Javascript
微信小程序日历插件代码实例
2019/12/04 Javascript
解决Vue @submit 提交后不刷新页面问题
2020/07/18 Javascript
python递归计算N!的方法
2015/05/05 Python
Python实现七彩蟒蛇绘制实例代码
2018/01/16 Python
详解Python核心对象类型字符串
2018/02/11 Python
tensorflow: 查看 tensor详细数值方法
2018/06/13 Python
tensorflow 只恢复部分模型参数的实例
2020/01/06 Python
python实现猜数游戏
2020/03/27 Python
Python super()函数使用及多重继承
2020/05/06 Python
python实现在线翻译
2020/06/18 Python
Python3爬虫发送请求的知识点实例
2020/07/30 Python
LookFantastic丹麦:英国美容护肤精品在线商城
2016/08/18 全球购物
优衣库澳大利亚官网:UNIQLO澳大利亚
2017/01/18 全球购物
德国电子商城:ComputerUniverse
2017/04/21 全球购物
eDreams意大利:南欧领先的在线旅行社
2018/11/23 全球购物
Swanson中国官网:美国斯旺森健康产品公司
2021/03/01 全球购物
篮球比赛口号
2014/06/10 职场文书
上课迟到检讨书范文
2015/05/06 职场文书
《圆明园的毁灭》教学反思
2016/02/16 职场文书
Python学习开发之图形用户界面详解
2021/08/23 Python