ecshop如何调用顶级栏目下面的子分类,元素模板为您解答
在根目录下category.php中添加代码
//当前二级分类 $smarty->assign('categories1', get_categories(get_parent($cat_id))); $smarty->assign('parent_id', get_parent($cat_id));//顶级分类id
在category.php 最后添加以下函数
/*获取顶级id*/ function get_parent($value,$id='') { if($value!=0) { $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$value'"; $res = $GLOBALS['db']->getOne($sql); return get_parent($res,$value); } else { return $id; } } /*提取二级分类*/ function get_categories($cat_id = 0) { if ($cat_id > 0) { $parent_id = $cat_id; } else { $parent_id = 0; } /* 判断当前分类中全是是否是底级分类, 如果是取出底级分类上级分类, 如果不是取当前分类及其下的子分类 */ $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id' AND is_show = 1 "; if ($GLOBALS['db']->getOne($sql) || $parent_id == 0) { /* 获取当前分类及其子分类 */ $sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, a.is_show,' . '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 AND b.is_show = 1 ' . "WHERE a.parent_id = '$parent_id' ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC"; } else { /* 获取当前分类及其父分类 */ $sql = 'SELECT a.cat_id, a.cat_name, b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order, b.is_show ' . 'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' . "WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC"; } $res = $GLOBALS['db']->getAll($sql); $cat_arr = array(); foreach ($res AS $row) { if ($row['is_show']) { $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'] != NULL) { $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; }
在根目录theme目录下的default下找到category.dwt
合适的位置添加 代码:
<!--品牌 start--> <!--{if $categories1}--> <div class="screeBox"> <strong>分类:</strong> <!-- {if $parent_id==$category}--> <span>全部</span> <!-- {else} --> <a href="category.php?id={$parent_id}">全部</a> <!-- {/if} --> <!--{foreach from=$categories1 item=cat}--> <!-- {if $category==$cat.id} --> <span>{$cat.name}</span> <!-- {else} --> <a href="{$cat.url}">{$cat.name}</a> <!-- {/if} --> <!--{/foreach}--> </div> <!--{/if}--> <!--end-->