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

时间:2012-11-11 13:18:01

标签: php mysql tree

我很难掌握创建类别树的概念。这是标准:

  • 创建无限深度的父/子HTML无序列表
  • 删除状态为= 0且所有子项
  • 的所有项目
  • 创建网址,例如/衣服/牛仔裤
  • 可选:生成面包屑

我能找到的最接近的解决方案让我更加困惑,因为我无法让它们发挥作用:

这是我的数组:

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>

2 个答案:

答案 0 :(得分:1)

可能是一个递归函数:

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,'/');

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

  • 衣服(/衣服)
  • -Jeans(/ clothes / jeans)
  • - 连衣裙(/衣服/连衣裙)
  • 配件(/配件)
  • -Bags(/ accessories / bags)
  • - 手表(/配件/手表)

(这是对此的改编版本:Storing Hierarchical Data in a Database

答案 1 :(得分:0)

好的,我为你提供了一个具有递归功能的解决方案。 如果您赶时间,可以找到完整的代码here

这篇文章的其余部分只是解释它是如何工作的。

这是你的阵列:

$arr = 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'
    ) 

) ;

下次在问题中包含数组时,请确保它已准备好在php脚本中使用,以便人们可以直接使用它。

现在,我首先通过阵列并建立父母和孩子之间的联系。我所做的是,如果一个id为1的元素有任何子元素,它的所有子元素都会进入数组的索引1.这就是为我做这个技巧的部分。

$tree = array ();
foreach ($arr as $val) {
    $tree [ $val['parent'] ] [] = $val;

}

非常简单吧??? :)

这是在处理后从数组生成的数组:

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

现在,主要部分进入场景。因为任何孩子也可以是一些孩子的父母,所以最简单的解决方案是递归。这是递归部分。

function traverser ($array ,$arr)  {


        if (! $arr)
            return;


    echo "<ul>" . "</br>";


    foreach ($arr as $var ) {
        echo "<li>" . "</br>";
        echo '<a href="/'.$var['slug'].'">'.$var['title'].'</a>';
        if (isset ($array [$var['id'] ]))
            traverser ($array , $array [$var['id'] ] ) ;

        echo "</li>";
    }


    echo "</ul>";

您可以找到完整的代码here

希望有所帮助。快乐的编码。