/**
* 获得指定分类下的子分类
*
* @access public
* @param integer $cat_id 分类编号
* @return array
*www.ebingou.cn
*/
function get_children_tree($cat_id)
{
if ($cat_id >0 )
{
$sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id'";
//$cot = $GLOBALS['db']->getOne($sql);
if ($GLOBALS['db']->getOne($sql))
{
// 获取当前分类名及其子类
$sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, ' .
'b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order AS child_order ' .
'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id ' .
"WHERE a.cat_id = '$cat_id' ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC";
}
else
{
$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
$parent_id = $GLOBALS['db']->getOne($sql);
if ($parent_id > 0)
{
//获取当前分类、兄弟及其父类
$sql = 'SELECT a.cat_id, a.cat_name, b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order ' .
'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id ' .
"WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC";
}
else
{
//获取当前分类
$sql = 'SELECT a.cat_id, a.cat_name FROM '
. $GLOBALS['ecs']->table('category') . ' AS a ' .
"WHERE a.cat_id = '$cat_id'";
}
}
$res = $GLOBALS['db']->getAll($sql);
$cat_arr = array();
foreach ($res AS $row)
{
$cat_arr[$row['cat_id']]['id'] = $row['cat_id'];
$cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
$cat_arr[$row['cat_id']]['url'] = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
if ($row['child_id'] !NULLbr /> {
$cat_arr[$row['cat_id']]['children'][$row['child_id']]['id'] = $row['child_id'];
$cat_arr[$row['cat_id']]['children'][$row['child_id']]['name'] = $row['child_name'];
$cat_arr[$row['cat_id']]['children'][$row['child_id']]['url'] = build_uri('category', array('cid' => $row['child_id']), $row['child_name']);
}
}
return $cat_arr;
}
}
这其实就是一个get_children_tree函数,更具$cat_id来得到当前分类所有的分类
$smarty->assign('categories', get_categories_tree($cat_id)); // 分类树
这其实是模板技术,如果你想 抛弃原来的分类样式,那么把get_categories_tree($cat_id)换成刚才我们自定义的函数get_children_tree($cat_id)
$smarty->assign('categories2', get_children_tree($cat_id));
如果想用不同的颜 表示出当前点击的分类和其他分类,那么还要保留当前点击的分类id。再加一行:
$smarty->assign('current_cat_id',
$cat_id);
//当前的id
最后一步修改category.dwt。
{$cat.name|escape:html}
· {$child.name|escape:html}
· 没有分类了!
版权声明: 本站资源均来自互联网或会员发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!
转载请注明: ecshop商品分类显示商品分类菜单的方法