Home > Backend Development > PHP Tutorial > 数据库分类查询有关问题

数据库分类查询有关问题

WBOY
Release: 2016-06-13 12:59:36
Original
869 people have browsed it

数据库分类查询问题
有一个分类表,结构是这样的 
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分组排序

<br />
SELECT * from good<br />
ORDER BY series<br />
Copy after login


------解决方案--------------------
<br />
<br />
static function GetTree($depth, $parentID, $nodeID)<br />
    {<br />
<br />
        $str = "";<br />
        $nodes = D_node::GetListAll("where parentid=" . $parentID . "", "orderflag desc");<br />
        if (count($nodes) > 0) {<br />
            foreach ($nodes as $node) {<br />
                $str .= self::GetPrefixString($depth) . $node->nodename;<br />
                $str .= self::GetTree($depth + 1, $node->id, $nodeID);<br />
            }<br />
        }<br />
<br />
<br />
        return $str;<br />
    }<br />
    static function GetPrefixString($depth)<br />
    {<br />
<br />
        $str = "--";<br />
        for ($i = 0; $i < $depth; $i++) {<br />
            $str .= "--";<br />
<br />
        }<br />
        return $str;<br />
    }<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template