id pid name haschild
一直用这个方法haschild表示是否有子节点!
一直用这个方法haschild表示是否有子节点!
解决方案 »
- 两个Parse error: syntax error, unexpected T_VARIABLE 错误
- 求php上传视频的代码,视频格式:flv
- phpmyadmin导出导入问题
- 关于php处理ajax请求的困惑
- MYSQL有没有自动切换服务器的功能
- 帮我看看这个表达式哪里错了
- CGI Error:The specified CGI application misbehaved by not returning a complete set of HTTP headers.这是什么错误?
- 为什么始终有个记录读不出来?
- 请帮我看下这段代码
- 在客服端抓取服务器本地的url怎么提高速度??
- php中正则表达是匹配除数字以外的所有字符.
- php 变量的问题
就这三个字段可以做
可以不用递归
就这三个字段可以做
可以不用递归利用数组的特性来做
http://topic.csdn.net/u/20090312/19/998C593D-344A-42AE-B263-E9FF293299EE.html
这个里面的讨论也有我举的例子
<?php
//无限分类类
class sortClass
{
/*
作者:笑看风云
mainlTo:[email protected]
如果有什么疑问和bug请指出发到我的邮箱大家一起讨论
getTree();返回排序好的节点数组,每个节点的深度已经整理好了
getList($root,$self);//获取指定节点下面的子节点,$root指节点id,self指是否包括自身节点id,0为不包括,默认为1
getList(0,0)获取所有节点,不包括“顶级节点”,因为顶级节点没有存放到lineRs数组当中去,一般也用不到
其它情况正常使用
getPosition($id,$self=1);返回当前位置,默认包括自己
*/
public $child = array();
public $parent = array();
public $name = array();
public $lineRs = array();//行记录
function __construct($sql)//构造函数
{
global $db;
$this->setNode(0, -1, '顶极节点');
$result = $db->query($sql);
foreach($result as $val)
{
$this->lineRs[$val["id"]] = $val;
$this->setNode($val["id"],$val["fid"]);
}
$this->initDepth();
}
function getTree()
{
return $this->lineRs;//构造函数返回初始化好的节点数组
}
function setNode($id,$parent)
{
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
}
function initDepth($root=0,$depth=0)//初始化节点深度
{
if (!isset($this->child[$root])) return;
foreach ($this->child[$root] as $key=>$id)
{
$this->lineRs[$id]["depth"] = $depth;
if (isset($this->child[$id]))
{
if ($this->child[$id]) $this->initDepth($id,$depth+1);
}
}
}
function getList($root=0,$self=1)
{
$tree = array();
if($self == 1)
{
$tree[] = $root;
}
$this->getList_1($root,$tree);
return $tree;
}
function getList_1($root,&$tree)
{
if (!isset($this->child[$root])) return;
foreach ($this->child[$root] as $key=>$id)
{
$tree[] = $id;
if (isset($this->child[$id]))
{
$this->getList_1($id,$tree);
}
}
}
function getPosition($id,$self=1)
{
$position = array();
if($self == 1)
{
$position[] = $id;
}
while($this->parent[$id] != 0)
{
$id = $this->parent[$id];
$position[] = $id;
}
//return implode(array_reverse($position)," → ");
return array_reverse($position);
}
}
//使用方法
//include("include.php");
//$sql = "select * from aa order by orderId asc,id asc";
//$sort = new sortClass($sql);
//$tree = $sort->getTree();
//$childes = $sort->getList(0,0);
//foreach($childes as $key=>$id)
//{
// echo str_repeat("-",$tree[$id]["depth"]) . $tree[$id]["name"] . "<br>";
//}
//echo "<hr>";
//$postion = $sort->getPosition(5);
//foreach($postion as $key=>$id)
//{
// echo $tree[$id]["name"] . " -> ";
//}
?>