日期:2014-05-17  浏览次数:20436 次

数据库分类查询问题
有一个分类表,结构是这样的 
id     name     father     categoryorder
1       A         0              
2       B         0              
3      A.1        1
4      A.2        1
5      A.1.a      3
6      A.1.b      3
7      B.1        2

categoryorder我想用于分类的排序,想实现把分类以树形显示的功能,categoryorder应该怎么设计?
A
--A.1
----A.1.a
----A.1.b
--A.2
B
--B.1
在PHP上应该怎么实现?
------解决方案--------------------
LZ如果你的"分类表"是数据库表,你应该考虑现在SQL做处理。
按你的思路,你可以建一个字段叫"系列"之类的 根据这个系列用order by分组排序

SELECT * from good
ORDER BY series


------解决方案--------------------


static function GetTree($depth, $parentID, $nodeID)
    {

        $str = "";
        $nodes = D_node::GetListAll("where parentid=" . $parentID . "", "orderflag desc");
        if (count($nodes) > 0) {
            foreach ($nodes as $node) {
                $str .= self::GetPrefixString($depth) . $node->nodename;
                $str .= self::GetTree($depth + 1, $node->id, $nodeID);
            }
        }


        return $str;
    }
    static function GetPrefixString($depth)
    {

        $str = "--";
        for ($i = 0; $i < $depth; $i++) {
            $str .= "--";

        }
        return $str;
    }