我在smarty框架下写了个js无限级菜单,按照样式通过循环写,但是遇到问题了,就是循环到最后一个记录的时候变换样式,如果遇到有两个记录的话第一个样式与第二个样式不一样。树形菜单效果图:而我写出来的效果图:
在dfsfdfs下面就只有两个子类,下面再没有了。而在r4342下面还有一个子类却没有出来。
我自己写的无限级函数代码:/**
* +----------------------------------------------------------
* 获取有层次的栏目分类,有几层分类就创建几维数组
* +----------------------------------------------------------
* $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>
在dfsfdfs下面就只有两个子类,下面再没有了。而在r4342下面还有一个子类却没有出来。
我自己写的无限级函数代码:/**
* +----------------------------------------------------------
* 获取有层次的栏目分类,有几层分类就创建几维数组
* +----------------------------------------------------------
* $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>
并且 smarty 也不支持递归(需要自己写函数,网上有)你给出的效果图中看到的是图标的变化,但你的模版中并没有看到相关代码