喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

[北京]代朋友公司招聘PHP高级程序员多名

上一主题 下一主题
     
标题: 发个通过左右节点来实现无级分类办法  
 
hans
中级会员
Rank: 3Rank: 3
老会员


UID 19962
精华 0
积分 445
帖子 457
金钱 445 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2003-5-19
来自 上海 浦东
状态 离线
发个通过左右节点来实现无级分类办法



<?php
/*
    tree.class.php
    @Author: enze   [email]enze@sina100.com[/email]  [email]enzewei@gmail.com[/email]
*/

//暂时没有做更新菜单
/*
调用显示
$f=array(0=>'id',1=>'name',2=>'lev');
$w='where pause=1';
$tree=new Tree('news','news_sort');
$sort=$tree->show_tree($f,$w);
foreach($sort as $sortname)
{
    unset($space);
    for($i=0;$i<$sortname->lev;$i++)
    {
        $space.='&nbsp;';
    }            
    $sortname->name=$sortname->lev>0?$space.'├&nbsp;'.$sortname->name:$sortname->name;
    $sortname->id=$id==$sortname->id?$sortname->id.' selected':$sortname->id;
}


添加

$f=array(0=>'fid',1=>'name',2=>'dirname');
$v=array(0=>"'$fid'",1=>"'$name'",2=>"'$ename'");

$tree=new Tree('news','news_sort');
$tree->add_tree($f,$v,$fid);

*/
Class Tree
{
    
//声明一些变量
    
var $db;
    var 
$sql;
    var 
$query;
    var 
$num;
    var 
$result=array();
    var 
$array_num;
    var 
$res;
    
    
//类同名函数,调用类时自动执行。
    //$dbname是数据库名称
    //$table是数据库表
    
function Tree($dbname='',$table='')
    {
        
$this->dbname=$dbname;
        
$this->table=$table;
    }

    
//以数组的形式返回树状菜单
    //filed是查询数据库的字段为数组,where是限制条件

    
function show_tree($filed,$where='')
    {
        
$this->array_num=count($filed);
        
$fileds=$this->array_num>1?join(',',$filed):$filed[0];
        
$this->db=new DataBase($this->dbname);
        
$this->sql="select $fileds from `$this->table` $where order by `lft`";
        
$this->query=$this->db->query($this->sql);
        
$this->num=$this->db->query_num($this->query);
        if(
$this->num<1)
        {
            return 
false;
        }
        for(
$i=0;$i<$this->num;$i++)
        {
            
$result[]=$this->db->query_object($this->query);
        }
        return 
$result;
    }

    
//添加菜单,$filed,$value为数组,即字段名,值
    //$fid为当前添加菜单的父,根的时候为0

    
function add_tree($filed,$value,$fid)
    {
        
//获取左右节点
        
$lft=$this->__get_lft($fid);
        
$rgt=$this->__get_rgt($fid);

        
//下面两个是方便显示与排序用的。
        
$lev=$this->__get_level($fid);
        
$oid=$this->__get_order($fid);

        
//压入数组
        
array_push($filed,'lft');
        
array_push($filed,'rgt');
        
array_push($filed,'lev');
        
array_push($filed,'oid');
        
array_push($value,$lft);
        
array_push($value,$rgt);
        
array_push($value,$lev);
        
array_push($value,$oid);

        
//更新节点
        
$this->__update_node($lft,$rgt);
        
        
//取出sql语句
        
$this->array_num=count($filed);
        
$fileds=$this->array_num>1?join(',',$filed):$filed[0];
        
$this->array_num=count($value);
        
$values=$this->array_num>1?join(',',$value):$value[0];

        
$this->db=new DataBase($this->dbname);

        
$this->sql="insert into `$this->table` ($fileds) values ($values)";

        
$this->query=$this->db->query($this->sql);
    }

    
//获取左节点
    
function __get_lft(&$fid)
    {        
        
$this->db=new DataBase($this->dbname);
        
$this->sql="select `rgt` from $this->table where `fid` = '$fid' order by lft desc limit 1";
        
$this->query=$this->db->query($this->sql);
        
$this->num=$this->db->query_num($this->query);
        
//当前菜单的父菜单具有子菜单
        
if($this->num>0)
        {
            
$this->res=$this->db->query_object($this->query);
            
//这里是同级菜单的右节点+1
            
$lft=$this->res->rgt+1;
        }
        else
        {
            
$this->sql="select `lft` from $this->table where `id` = '$fid' order by id";
            
$this->query=$this->db->query($this->sql);
            
$this->num=$this->db->query_num($this->query);
            
//父菜单暂时没有子菜单
            
if($this->num==1)
            {
                
$this->res=$this->db->query_object($this->query);
                
//这里是父菜单左节点+1
                
$lft=$this->res->lft+1;
            }
            else
            {
                
//第一个菜单
                
$lft=1;
            }
        }
        return 
$lft;
    }
    
    
//获取右节点
    
function __get_rgt(&$fid)
    {
        
$this->db=new DataBase($this->dbname);
        
$this->sql="select `rgt` from $this->table where `fid` = '$fid' order by lft desc limit 1";
        
$this->query=$this->db->query($this->sql);
        
$this->num=$this->db->query_num($this->query);
        
//当前菜单的父菜单具有子菜单
        
if($this->num>0)
        {
            
$this->res=$this->db->query_object($this->query);
            
//这里是同级菜单的右节点+2
            
$rgt=$this->res->rgt+2;
        }
        else
        {
            
$this->sql="select `rgt` from $this->table where `id` = '$fid' order by id";
            
$this->query=$this->db->query($this->sql);
            
$this->num=$this->db->query_num($this->query);
            
//父菜单暂时没有子菜单
            
if($this->num==1)
            {
                
$this->res=$this->db->query_object($this->query);
                
//这里是父菜单右节点+1
                
$rgt=$this->res->rgt+1;
            }
            else
            {
                
//第一个菜单
                
$rgt=2;
            }
        }
        return 
$rgt;
    }
    
    
//更新节点
    
function __update_node(&$lft,&$rgt)
    {
        
$this->db=new DataBase($this->dbname);
        
$this->sql="update $this->table set `lft`=`lft`+2 where `lft`>='$lft'";
        
$this->query=$this->db->query($this->sql);
        
$this->sql="update $this->table set `rgt`=`rgt`+2 where `rgt`>='$lft'";
        
$this->query=$this->db->query($this->sql);
    }
    
    
//获取菜单的级别
    
function __get_level(&$fid)
    {
        
$this->db=new DataBase($this->dbname);
        
$this->sql="select `lev` from $this->table where `id` = '$fid' order by id";
        
$this->query=$this->db->query($this->sql);
        
$this->num=$this->db->query_num($this->query);
        if(
$this->num>0)
        {
            
$this->res=$this->db->query_object($this->query);
            
$lev=$this->res->lev+1;
        }
        else
        {
            
$lev=0;
        }
        return 
$lev;
    }
    
    
//获取菜单的排序
    
function __get_order(&$fid)
    {
        
$this->db=new DataBase($this->dbname);
        
$this->sql="select `oid` from $this->table where `fid` = '$fid' order by id desc limit 1";
        
$this->query=$this->db->query($this->sql);
        
$this->num=$this->db->query_num($this->query);
        if(
$this->num>0)
        {
            
$this->res=$this->db->query_object($this->query);
            
$oid=$this->res->oid+1;
        }
        else
        {            
            
$oid=1;
        }
        return 
$oid;
    }
}
?>
2006-7-18 11:00 AM#1
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
goshawk
注册会员
Rank: 2
初级会员


UID 67921
精华 0
积分 71
帖子 513
金钱 71 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-1-10
状态 离线
[推荐阅读] 页面的数据查询的新问题
有没有例子啊
2006-7-18 11:42 AM#2
查看资料  Blog  发短消息  QQ  顶部
 
Jaycn
注册会员
Rank: 2



UID 69863
精华 0
积分 189
帖子 204
金钱 189 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-3-19
状态 离线
[推荐阅读] 问:怎么用JS操作CSS里的z-index?
<?
调用显示
$f=array(0=>'id',1=>'name',2=>'lev');
$w='where pause=1';
$tree=new Tree('news','news_sort');
$sort=$tree->show_tree($f,$w);
foreach($sort as $sortname)
{
    unset($space);
    for($i=0;$i<$sortname->lev;$i++)
    {
        $space.=' ';
    }            
    $sortname->name=$sortname->lev>0?$space.'├ '.$sortname->namesortname->name;
    $sortname->id=$id==$sortname->id?$sortname->id.' selected'sortname->id;
}


添加

$f=array(0=>'fid',1=>'name',2=>'dirname');
$v=array(0=>"'$fid'",1=>"'$name'",2=>"'$ename'");

$tree=new Tree('news','news_sort');
$tree->add_tree($f,$v,$fid);
?>




流星下的天空,一个专注WAP的网站,J2ME技术 http://www.onlywap.cn
2006-7-18 12:15 PM#3
查看资料  发短消息  顶部
 
c1a1o1
版主
Rank: 7Rank: 7Rank: 7
一品带刀护卫


UID 68669
精华 0
积分 1225
帖子 2180
金钱 1225 喜悦币
威望 0
人脉 0
阅读权限 100
注册 2006-2-17
状态 离线
[推荐阅读] 使用Header重定向后以前的session是否依然存在
不错。



成人电影
php教材价格比较网美女图片
2006-7-18 01:22 PM#4
查看资料  Blog  发短消息  QQ  顶部
 
sanders_yao
版主
Rank: 7Rank: 7Rank: 7
or2 =333


UID 30286
精华 0
积分 2531
帖子 4699
金钱 2528 喜悦币
威望 0
人脉 3
阅读权限 100
注册 2004-7-23
来自 北京 菜户营
状态 离线
[推荐阅读] 再问ajax问题
我一直感觉这种结构仅仅适合获取下面的所有节点
但不适合获取下面一级的节点
而且如果移动某节点到其他节点下面操作会相当麻烦
我最近也在做类似的东西欢迎不同意见来探讨
等完善自己的代码之后若有机会会贴上来




最近写的一些代码都会在以下地址发布,有兴趣的来批:
http://www.phpx.com/happy/thread-131368-1-1.html
2006-7-18 01:27 PM#5
查看资料  Blog  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.042579 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP