21,886
社区成员
发帖
与我相关
我的任务
分享
/**
* +----------------------------------------------------------
* 获取有层次的栏目分类,有几层分类就创建几维数组
* +----------------------------------------------------------
* $table 数据表名
* $parent_id 默认获取一级导航
* $current_id 当前页面栏目ID
* +----------------------------------------------------------
*/
function get_category($table, $parent_id = 0, $current_id = '') {
$category = array ();
$data = $this->fetch_array_all($table, 'sort ASC');
foreach ((array) $data as $value) {
// $parent_id将在嵌套函数中随之变化
if ($value['parent_id'] == $parent_id) {
$value['url'] = $this->rewrite_url($table, $value['cat_id']);
$value['cur'] = $value['cat_id'] == $current_id ? true : false;
foreach ($data as $child) {
// 筛选下级导航
if ($child['parent_id'] == $value['cat_id']) {
// 嵌套函数获取子分类
$value['child'] = $this->get_category($table, $value['cat_id'], $current_id);
break;
}
}
$category[] = $value;
}
}
return $category;
}
<div id="LeftNav">
<div id="leftnavdiv">
<ul id="mktree" class="ulShow">
{foreach from=$article_list name=art1 key=i item=art1}
<li>{if $smarty.foreach.art1.last=="1"}<span></span>{else}<em></em>{/if}<a {if $smarty.foreach.art1.last=='1'}href="{$art1.url}"{/if} target="mainFrame">{$i+1} {$art1.cat_name}</a>
<ul>
{foreach from=$art1.child name=art2 item=art2}
<li>{if $smarty.foreach.art2.last=="1"}<span></span>{else}<em></em>{/if}<a {if $smarty.foreach.art2.last=='1'}href="{$art2.url}"{/if} target="mainFrame">{$art2.cat_name}</a>
<ul>
{foreach from=$art2.child name=art3 item=art3}
<li>{if $smarty.foreach.art3.last=="1"}<span></span>{else}<em></em>{/if}<a {if $smarty.foreach.art3.last=='3'}href="{$art3.url}"{/if} target="mainFrame">{$art3.cat_name}</a>
{if $art3.child}
<ul>
{foreach from=$art3.child name=art4 item=art4}
<li>{if $smarty.foreach.art4.last=="1"}<span></span>{else}<em></em>{/if}<a {if $smarty.foreach.art4.last=='1'}href="{$art4.url}"{/if} target="mainFrame">{$smarty.foreach.art4.index+1}.{$art4.cat_name}</a></li>
{/foreach}
</ul>
{/if}
</li>
{/foreach}
</ul></li>
{/foreach}
</ul></li>
{/foreach}
</ul>