我能够生成输出为:
Cat1
SubCat1
SubCat3
但是我需要输出为
Cat1
Subcat1
SubCat2
SubCat3
为了构建我的菜单,我使用 db 结构:
site_id | parent_id | site_name | site_url | Level
1 | 0 | Category 1 |example.com| 0
2 | 1 | Sub Cat 1 |example.com| 1
3 | 2 | Sub Cat 2 |example.com| 2
4 | 1 | Sub Cat 3 |example.com| 1
我能够将我的数组生成为:
Array (
[0] => Array (
[0] => 63
[site_id] => 63
[1] => 0
[parent_id] => 0
[2] => Category 1
[site_name] => Category 1
[3] => Link1
[site_url] => Link1
[4] => 0
[level] => 0
[children] => Array (
[0] => Array (
[0] => 66
[site_id] => 66
[1] => 63
[parent_id] => 63
[2] => Sub Cat 3
[site_name] => Sub Cat 3
[3] =>
[site_url] =>
[4] => 1
[level] => 1
[children] => Array ( )
)
[1] => Array (
[0] => 64
[site_id] => 64
[1] => 63
[parent_id] => 63
[2] => Sub Cat 1
[site_name] => Sub Cat 1
[3] =>
[site_url] =>
[4] => 2
[level] => 2
[children] => Array (
[0] => Array (
[0] => 65
[site_id] => 65
[1] => 64
[parent_id] => 64
[2] => Sub Cat2
[site_name] => Sub Cat2
[3] =>
[site_url] =>
[4] => 1
[level] => 1
[children] => Array ( )
)
)
)
)
)
)
使用来自:https ://stackoverflow.com/a/2795069/1137983 的函数。我只使用了帖子中的 getTopCategories() 和 getCategories 函数
为了生成输出,我使用:
{foreach from=$sitemap item=c name=sitemap}
{if $c.level==0 }
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
{foreach item=d from=$c.children name=sitemap}
<li><a title="{$d.site_name}" href="{$d.site_url}">{$d.site_name}</a></li>
{/foreach}
{else}
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
{/if}
</ul>
</li>
{/foreach}
</ul>