是做广告吗?
无限分类树,可以google一下

解决方案 »

  1.   

    <?php
    //无限分类类
    class sortClass
    {
    /*
    getTree();返回排序好的节点数组,每个节点的深度已经整理好了
    getList($root,$self);//获取指定节点下面的子节点,$root指节点id,self指是否包括自身节点id,0为不包括,默认为1
    getList(0,0)获取所有节点,不包括“顶级节点”,因为顶级节点没有存放到lineRs数组当中去,一般也用不到
    其它情况正常使用
    getPosition($id,$self=1);返回当前位置,默认包括自己
    getRootFid($id)//获取当前分类的最顶级分类id
    */
    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);
    }
    function getRootFid($id)
    {
    while($this->parent[$id] != 0)
    {
    $id = $this->parent[$id];
    }
    return $id;
    }
    }
    //使用方法
    //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"] . " -> ";
    //}
    ?>