id pid name haschild
一直用这个方法haschild表示是否有子节点!

解决方案 »

  1.   

    id pid name 
    就这三个字段可以做
    可以不用递归
      

  2.   

    id pid name 
    就这三个字段可以做
    可以不用递归利用数组的特性来做
      

  3.   

    http://topic.csdn.net/u/20090311/15/637DE3A0-F2AB-4F5A-BDB2-D2B726F8F6C7.html至于上面的做法,你可以参考这个帖子里面我发的这个优点就是没有冗余字段,缺点是排序相对难做关于上面那个的排序问题可以参考
    http://topic.csdn.net/u/20090312/19/998C593D-344A-42AE-B263-E9FF293299EE.html
    这个里面的讨论也有我举的例子
      

  4.   


    <?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"] . " -> ";
    //}
    ?>