麻辣堂|资源主站|开发论坛|在线手册
首页 Apache Linux Java MySQL 注册帮助 
PHP项目开发组是PHP开发资源网于2007组年建成立的项目开发团队,目前核心开发成员有27人, 项目协作成员8名.下设7个开发组,主要承接大/中型网站项目开发任务。

    由于开发任务较多,人员比较紧张,现面向社会招聘全职或者兼职开发人员,不管你是在校大学生,还是全职开发人员,以及SOHO都可以联系本站,我们可以长期合作,并为您带来丰厚的报酬。
  您现在的位置:PHP开发资源网 > 麻辣堂 > 详细资料
待解决
发个通过左右节点来实现无级分类办法
悬赏分:20 - 2007年08月22日

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

//暂时没有做更新菜单
/*
调用显示
$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);

*/
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;
}
}
?>
[/php]

提问者:hans   08-22 08:08
答复
路过。。。顺便帮顶:)
回答者:玉米づ冰冻可乐 - 瓦岗村民 8-22 09:10
我也来回答:
不管你有没有帮助我们,瓦岗寨8万村民将感谢你。。。。。

为防止灌水,您需要计算一道数学题: 答案:
86 + 19 = ? 请将计算结果填在上面

 
[]
©2007 PhpRes.COM