在includes/cls_template.php里顶部插入分类函数代码:
//通过参数判断是否存在二级分类
function get_subcate_byurl($url)
{
$rs = strpos($url,"category");
if($rs!==false)
{
preg_match("/\d+/i",$url,$matches);
$cid = $matches[0];
$cat_arr = array();
$sql = "select * from ".$GLOBALS['ecs']->table('category')." where parent_id=".$cid." and is_show=1 ORDER BY sort_order ASC, cat_id ASC";
$res = $GLOBALS['db']->getAll($sql);
foreach($res as $idx => $row)
{
$cat_arr[$idx]['id'] = $row['cat_id'];
$cat_arr[$idx]['name'] = $row['cat_name'];
$cat_arr[$idx]['url'] = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
$cat_arr[$idx]['children'] = get_clild_list($row['cat_id']);
}
return $cat_arr;
}
else
{
return false;
}
}
function get_clild_list($pid)
{
//开始获取子分类
$sql_sub = "select * from ".$GLOBALS['ecs']->table('category')." where parent_id=".$pid." and is_show=1 ";
$subres = $GLOBALS['db']->getAll($sql_sub);
if($subres)
{
foreach ($subres as $sidx => $subrow)
{
$children[$sidx]['id']=$subrow['cat_id'];
$children[$sidx]['name']=$subrow['cat_name'];
$children[$sidx]['url']=build_uri('category', array('cid' => $subrow['cat_id']), $subrow['cat_name']);
}
}
else
{
$children = null;
}
return $children;
}
接下来呢,ecshop中的模板文件,也就是thems,找到你自己的模板文件夹,我就使用默认的说好了,打开themes\default\library\page_header.lbi文件
找到ecshop的导航条模板文件代码,也就是以下代码:
<li><a href="../index.php"{if $navigator_list.config.index eq 1} class="cur"{/if}>{$lang.home}<span></span></a></li>
<!-- {foreach name=nav_middle_list from=$navigator_list.middle item=nav} -->
<li><a href="{$nav.url}" {if $nav.opennew eq 1}target="_blank" {/if} {if $nav.active eq 1}class="cur" {/if} >{$nav.name}<span></span></a></li>
<!-- {/foreach} -->
</ul>
在代码中加入一段调用分类的代码,位置看你如何构思二级分类的表现,我是直接这样加的,我是直接使用鼠标移到或移出当前栏目名称时,更改当前栏目标签的CLASS名称和CSS样式结合达到二级栏目的显示和隐藏的:
<li><a href="../index.php"{if $navigator_list.config.index eq 1} class="cur"{/if}>{$lang.home}<span></span></a></li>
<!-- {foreach name=nav_middle_list from=$navigator_list.middle item=nav} -->
<li class="menu2" id="netxu">
<a href="{$nav.url}" {if $nav.opennew eq 1}target="_blank" {/if} {if $nav.active eq 1}class="cur" {/if} >{$nav.name}<span></span></a>
<?php
$subcates = get_subcate_byurl($GLOBALS['smarty']->_var['nav']['url']);
if($subcates!=false)
{
if(count($subcates)>0)
{
if($subcates)
{
$i = 1;
echo "<p class='cnav' id='cnav'>";
foreach($subcates as $cate)
{
if($i % 2 == 0)
{
}
else
{
}
echo "<p class='class_box'>";
echo "<a class='level_1' href='".$cate['url']."' >".$cate['name']."</a>";
echo "</p>";
if($cate['children'])
{
foreach($cate['children'] as $subcate)
{
echo "<p class='three'>";
echo "<a href='".$subcate['url']."' class='level_2'>".$subcate['name']."</a>";
echo "</p>";
}
echo "</p>";
}
}
echo "</p>";
}
}
}
?>
</li>
<!-- {/foreach} -->
</ul>
CSS样式的表现思路:我通过鼠标移入移出改变当前li标签的CLASS名称,当鼠标移到上面时,CLASS为"menu1",当鼠标移出时,则变回"menu2",在li下的p层的显示和隐藏就决定该DIV的父层li 的CLASS名是"menu1"还是"menu2"了,2为隐藏,1为显示,具体就这样了,还看不懂的可以留言给我