将类别树生成为HTML无序列表

将类别树生成为HTML无序列表

本文介绍了将类别树生成为HTML无序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解创建类别树的概念.这是标准:

I have a really hard time grasping the concept of creating a category tree. This is the criterias:

  • 创建深度不受限制的父/子HTML无序列表
  • 删除状态为0的所有项目以及所有子项
  • 创建网址,例如/clothes/jeans
  • 可选:生成面包屑

我找不到的最接近的解决方案使我更加困惑,因为我无法使它们起作用:

The closest solution I could find just made me more confused because I couldn't make them work:

  • Convert a series of parent-child relationships into a hierarchical tree?
  • Creating a recursive category tree function

这是我的阵列:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [status] => 2
            [slug] => clothes
            [title] => Clothes
        )
    [1] => Array
        (
            [id] => 2
            [parent] => 1
            [status] => 2
            [slug] => jeans
            [title] => Jeans
        )
    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [status] => 2
            [slug] => dresses
            [title] => Dresses
        )
    [3] => Array
        (
            [id] => 4
            [parent] => 0
            [status] => 2
            [slug] => accessories
            [title] => Accessories
        )
    [4] => Array
        (
            [id] => 5
            [parent] => 4
            [status] => 2
            [slug] => bags
            [title] => Bags
        )
    [5] => Array
        (
            [id] => 6
            [parent] => 4
            [status] => 2
            [slug] => watches
            [title] => Watches
        )
    [6] => Array
        (
            [id] => 7
            [parent] => 6
            [status] => 2
            [slug] => rolex
            [title] => Rolex
        )
)

这是我想要的无序列表:

<ul>
    <li><a href="/clothes">Clothes</a>
        <ul>
            <li>
                <a href="/clothes/jeans">Clothes</a>
            </li>
            <li>
                <a href="/clothes/dresses">Clothes</a>
            </li>
        </ul>
    </li>
    <li><a href="/accessories">Accessories</a>
        <ul>
            <li>
                <a href="/accessories/bags">Bags</a>
            </li>
            <li>
                <a href="/accessories/watches">Watches</a>
                <ul>
                    <li>
                        <a href="/accessories/watches/rolex">Rolex</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

推荐答案

也许是这样的递归函数:

maybe a recursive function as this:

function filter_by_parent($parent_id,$ar){
    $retval=array();
    foreach($ar as $a){
        if($a['status']==0)
            continue;
        if($a['parent']==$parent_id)
            $retval[]=$a;
    }
    return $retval;
}

function print_list($parent, $level,$ar,$url_prefix) {
    $children = filter_by_parent($parent,$ar);
    if(empty($children))
        return;

    echo "<ul>";
    foreach($children as $child){
        // indent and display the title of this child <br>
        echo "<li>".$child['title']. "( {$url_prefix}{$child['slug']} )";

        print_list($child['id'], $level+1,$ar,$url_prefix.$child['slug'].'/');
        echo  "</li>";
    }
    echo "</ul>";
}
print_list(0,0,$test,'/');

根据您的输入,结果将是:

with your input, the result would be:

  • 衣服(/clothes)
  • -牛仔裤(/clothes/jeans)
  • -连衣裙(/clothes/dresses)
  • 附件(/accessories)
  • -包包(/accessories/bags)
  • -手表(/accessories/watches)

(这是此版本的改编版本:在数据库中存储分层数据)

(it's an adapted version of this: Storing Hierarchical Data in a Database)

这篇关于将类别树生成为HTML无序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:32