[php]无限级分类 要看的有木有!! | 张大奋的个人it技术博客

我是创始人李岩:很抱歉!给自己产品做个广告,点击进来看看。  

无级分类…看的有木有???
< ?php

class ccategorystruct {
    var $id;
    var $pid;
    var $order;
    var $depth;
    var $obj;
}

class ccategory {
    var $data;
    var $tdata;
    var $maxorder;

    function ccategory () {
        $this->data     = array();
        $this->tdata    = array();
        $this->maxorder = 0;
    }

# **************************************************
# 数据赋值
# **************************************************
    function assign ($arr, $f_id, $f_pid, $f_order) {
        if (is_array($arr)) {
            foreach ($arr as $v) {
                $this->assignonesortorder($v[$f_id], $v[$f_pid], $v[$f_order], $v);
            }
        } else {
            $this->assignonesortorder($arr, $f_id, $f_pid, $f_order);
        }
    }

# **************************************************
# 单条记录赋值
# **************************************************
    function assignone ($id, $pid, $order, $obj) {
        $data = new ccategorystruct;
        $data->id       = $id;
        $data->pid      = $pid;
        $data->order    = $order;
        $data->obj      = $obj;
        $this->data[]   = $data;
    }

# **************************************************
# 单条记录排序赋值
# **************************************************
    function assignonesortorder ($id, $pid, $order, $obj) {
        $data = new ccategorystruct;
        $data->id    = $id;
        $data->pid   = $pid;
        $data->order = $order;
        $data->obj   = $obj;
        if ($order < = $this->maxorder) {
            $orderdata = array();
            $arr       = &$this->data;
            $n         = count($arr);
            $i         = 0;
            while ($i < $n) {
                if ($order <= $arr[$i]->order) {
                    break;
                } else {
                    $orderdata[] = $arr[$i];
                }
                $i++;
            }
            $orderdata[] = $data;
            while ($i < $n) {
                $orderdata[] = $arr[$i];
                $i++;
            }
            $this->data = $orderdata;
        } else {
            $this->maxorder = $order;
            $this->data[]   = $data;
        }
    }

# **************************************************
# 数据排序
# **************************************************
    function datasortorder ($flag = ‘+’) {
        $arr = &$this->data;
        for ($i = 0; $i < count($arr); $i++) {
            for ($j = $i + 1; $j < count($arr); $j++) {
                if ( (($arr[$j]->order < $arr[$i]->order) && ($flag == ‘+’)) ||
                     (($arr[$j]->order > $arr[$i]->order) && ($flag == ‘-’)) ) {
                    $obj     = $arr[$i];
                    $arr[$i] = $arr[$j];
                    $arr[$j] = $obj;
                }
            }
        }
    }

# **************************************************
# 获得节点信息
# **************************************************
    function getnode ($id) {
        $arr = $this->data;
        foreach ($arr as $v) {
            if ($v->id == $id) return $v;
        }
    }

# **************************************************
# 获得子节点信息
# **************************************************
    function getchild ($id) {
        $arr  = $this->data;
        $data = array();
        foreach ($arr as $v) {
            if ($v->pid == $id) $data[] = $v;
        }
        return $data;
    }

# **************************************************
# 获得父节点信息
# **************************************************
    function getparent ($id) {
        $arr = $this->data;
        foreach ($arr as $v) {
            if ($v->id == $id) return $this->getnode($v->pid);
        }
    }

# **************************************************
# 获得全部子节点信息
# **************************************************
    function getchildren ($id = 0) {
        $n = count($this->tdata);
        if ($n > 0) {
            $data = array();
            $arr  = &$this->tdata;
            $i    = 0;
            while ($i < $n) {
                if ($arr[$i]->pid == $id) break;
                $i++;
            }
            if ($i == $n) return $data;
            $depth  = $arr[$i]->depth;
            while ($i < $n) {
                if ($arr[$i]->depth >= $depth) {
                    $data[] = $arr[$i];
                } else {
                    break;
                }
                $i++;
            }
            return $data;
        } else {
            $this->tdata = $this->gettreelist($id);
            return $this->tdata;
        }
    }

# **************************************************
# 获得全部子节点信息
# **************************************************
    function gettreelist ($id = 0, $depth = 0, &$tdata = array()) {
        $arr = $this->data;
        foreach ($arr as $v) {
            if ($v->pid == $id) {
                $v->depth = $depth;
                $tdata[]  = $v;
                $this->gettreelist($v->id, $depth + 1, $tdata);
            }
        }
        return $tdata;
    }

# **************************************************
# 获得全部父节点信息
# **************************************************
    function getparents ($id) {
        $data = array();
        while ($v = $this->getparent($id)) {
            $data[] = $v;
            $id     = $v->id;
        }
        return array_reverse($data);
    }

# **************************************************
# 比较两个节点的关系
# **************************************************
    function compare ($id1, $id2) {
        $data = $this->getchildren($id1);
        foreach ($data as $v) {
            if ($v->id == $id2) return ‘p’;
        }
        $data = $this->getparents($id1);
        foreach ($data as $v) {
            if ($v->id == $id2) return ‘c’;
        }
        $v1 = $this->getnode($id1);
        $v2 = $this->getnode($id2);
        if ($v1->pid == $v2->pid) return ‘s’;
        return ‘o’;
    }
}

?>

扫描雷锋网微信二维码
下载雷锋网ios客户端

随意打赏

提交建议
微信扫一扫,分享给好友吧。