ecshop怎么让分类页显示当前顶级分类的所有子分类?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ecshop怎么让分类页显示当前顶级分类的所有子分类?相关的知识,希望对你有一定的参考价值。
我想要的是,比如在首页点击一个顶级分类,进入分类页后,在商品列表的上面显示出当前顶级分类的子分类。能帮我一下吗?谢谢了
ecshop的商品分类页面category.php 下的分类,默认是取得所有同级父分类以及父类别的子分类。比如,我点击进入是A商品分类的页面 category.php?id=1,事实上 我只需要取得父ID为1的子分类即可,但是ecshop也把B商品分类、C商品分类.....下的所有子分类也输出来了。这是没必要的。在ecshop下的category.php 334行 $smarty->assign('categories', get_categories_tree($cat_id)); // 本身也是要起到这个作用,但是虽然有参数$cat_id,但是当$cat_id为顶级分类时候,该参数是无效的。为什么呢?我们来看一下 get_categories_tree( )这个函数(该函数在目录includes/lib_goods.php下)。如下:
/*** 获得指定分类同级的所有分类以及该分类下的子分类
*
* @access public
* @param integer $cat_id 分类编号
* @return array
*/
function get_categories_tree($cat_id = 0)
if ($cat_id > 0)
$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
$parent_id = $GLOBALS['db']->getOne($sql);
else
$parent_id = 0;
/*
判断当前分类中全是是否是底级分类,
如果是取出底级分类上级分类,
如果不是取当前分类及其下的子分类
*/
$sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$parent_id' AND is_show = 1 ";
if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
/* 获取当前分类及其子分类 */
$sql = 'SELECT cat_id,cat_name ,parent_id,is_show ' .
'FROM ' . $GLOBALS['ecs']->table('category') .
"WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
$res = $GLOBALS['db']->getAll($sql);
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 (isset($row['cat_id']) !=NULL
$cat_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);
if(isset($cat_arr))
return $cat_arr;
问题就在这一句 if ($cat_id > 0)
$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
$parent_id = $GLOBALS['db']->getOne($sql);
else
$parent_id = 0;
这一句是判断参数$cat_id是否有父类,若是有父类,就取出其父类的ID,否则视为参数为父类别ID为0,也即为顶级分类。事实上,当参数$cat_id大于0,并且为顶级分类的时候,这句话是无效的,我们假设$cat_id=1,且ID1为顶级ID,也即其parent_id 为0,这种情况下
$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
$parent_id = $GLOBALS['db']->getOne($sql);
运行得出的值还是0,也即$parent_id =0.虽然有参数,但还是取出所有顶级ID下的所有分类。实际上只需要这样修改即可,把
if ($cat_id > 0)
$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
$parent_id = $GLOBALS['db']->getOne($sql);
else
$parent_id = 0;
修改为
if ($cat_id > 0)$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
$parent_id = $GLOBALS['db']->getOne($sql);
if($parent_id==0)$parent_id=$cat_id;//添加上这句是关键。
else
$parent_id = 0;
这时候 参数$cat_id是有效的!
参考技术A <dl><dt><a href="$cat.url">$cat.name|escape:html</a></dt>
<!--foreach from=$cat.cat_id item=child-->
<dd><a href="$child.url">$child.name|escape:html</a></dd>
<!--foreach from=$child.cat_id item=childer-->
<dd> <a href="$childer.url">$childer.name|escape:html</a></dd>
<!--/foreach-->
<!--/foreach-->
</dl>
<!--/foreach-->
ecshop3 调用指定分类下推荐/热卖/新品商品,可指定调用数量
第一步:
--------------------------------------------------------------------------------------
/**
* 取指定分类ID及类型的商品信息
* @access public
* @param string $cat_id 分类ID
* @param string $num 显示商品数量
* @param string $cat_type 显示商品类型 new新品,hot热销,best为精品,promote特价
* @param auther 邓士鹏(自定义函数)
* @return array
*/
function index_get_cat_id_goods_list($cat_id = ‘‘, $num = ‘‘,$cat_type=‘‘)
{
$sql = ‘Select g.goods_id, g.cat_id,c.parent_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ‘ .
"IFNULL(mp.user_price, g.shop_price * ‘$_SESSION[discount]‘) AS shop_price, ".
"promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, " .
"g.is_best, g.is_new, g.is_hot, g.is_promote " .
‘FROM ‘ . $GLOBALS[‘ecs‘]->table(‘goods‘) . ‘ AS g ‘ .
‘LEFT JOIN ‘ . $GLOBALS[‘ecs‘]->table(‘category‘) . ‘ AS c ON c.cat_id = g.cat_id ‘ .
"LEFT JOIN " . $GLOBALS[‘ecs‘]->table(‘member_price‘) . " AS mp ".
"ON mp.goods_id = g.goods_id AND mp.user_rank = ‘$_SESSION[user_rank]‘ ".
"Where g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
$sql .= " AND (c.parent_id =" . $cat_id. " or g.cat_id = " . $cat_id ." or g.cat_id ". db_create_in(array_unique(array_merge(array($cat_id), array_keys(cat_list($cat_id, 0, false))))) .")";
if($cat_type!=‘‘){
$sql .=" and is_".$cat_type."=1";
}else{
$sql .=" and is_best=0 and is_new=0 and is_hot=0 and is_promote=0";
}
$sql .= " order by g.goods_id desc LIMIT $num";
$res = $GLOBALS[‘db‘]->getAll($sql);
$goods = array();
foreach ($res AS $idx => $row)
{
$goods[$idx][‘id‘] = $row[‘article_id‘];
$goods[$idx][‘id‘] = $row[‘goods_id‘];
$goods[$idx][‘name‘] = $row[‘goods_name‘];
$goods[$idx][‘brief‘] = $row[‘goods_brief‘];
$goods[$idx][‘brand_name‘] = $row[‘brand_name‘];
$goods[$idx][‘goods_style_name‘] = add_style($row[‘goods_name‘],$row[‘goods_name_style‘]);
$goods[$idx][‘short_name‘] = $GLOBALS[‘_CFG‘][‘goods_name_length‘] > 0 ?
sub_str($row[‘goods_name‘], $GLOBALS[‘_CFG‘][‘goods_name_length‘]) : $row[‘goods_name‘];
$goods[$idx][‘short_style_name‘] = add_style($goods[$idx][‘short_name‘],$row[‘goods_name_style‘]);
$goods[$idx][‘market_price‘] = price_format($row[‘market_price‘]);
$goods[$idx][‘shop_price‘] = price_format($row[‘shop_price‘]);
$goods[$idx][‘thumb‘] = empty($row[‘goods_thumb‘]) ? $GLOBALS[‘_CFG‘][‘no_picture‘] : $row[‘goods_thumb‘];
$goods[$idx][‘goods_img‘] = empty($row[‘goods_img‘]) ? $GLOBALS[‘_CFG‘][‘no_picture‘] : $row[‘goods_img‘];
$goods[$idx][‘url‘] = build_uri(‘goods‘, array(‘gid‘ => $row[‘goods_id‘]), $row[‘goods_name‘]);
}
return $goods;
}
?>
--------------------------------------------------------------------------------------
第二步:
打开根目录的 index.php 文件,在适当位置添加以下代码:
比如在
$smarty->assign(‘shop_notice‘, $_CFG[‘shop_notice‘]); // 商店公告
--------添加
// new新品,hot热销,best为精品,promote特价
$smarty->assign(‘cat_id1_new_goods‘, index_get_cat_id_goods_list(1,10));
$smarty->assign(‘cat_id1_hot_goods‘, index_get_cat_id_goods_list(1,10,‘hot‘));
$smarty->assign(‘cat_id1_hot_goods‘, index_get_cat_id_goods_list(1,10,‘promote‘));
$smarty->assign(‘cat_id1_hot_goods‘, index_get_cat_id_goods_list(1,10,‘best‘));
根据自己的需要添加多条。
其中,1为分类ID,10为调用的数量。
如果不限制商品分类,分类ID可填 0
注:如果需要在 分类页调用,则在 category.php 文件中添加。
如果需要在 商品详情页调用,则在 good.php 文件中添加。
==================================================
<!--{foreach name=cat_id1_hot_goods from=$cat_id1_hot_goods item=goods}-->
<div style="padding-top: 8px;" class="new-tr">
<a target="_blank" href="{$goods.url}"><img width="116" height="130" border="0" alt="{$goods.name|escape:html}" src="{$goods.thumb}"></a>
<div class="right">
<a target="_blank" href="{$goods.url}">{$goods.name|escape:html}</a><br>
<span style="color: rgb(102, 102, 102); text-decoration: line-through;">市场价:{$goods.market_price}</span><br>
特卖价:<span style="color: rgb(255, 0, 0);">{$goods.shop_price}</span><br>
<span class="font-gmm"><a href="javascript:addToCart({$goods.id})">立即抢购</a></span>
</div>
<span class="new-line"></span>
</div>
<!--{/foreach}-->
以上是关于ecshop怎么让分类页显示当前顶级分类的所有子分类?的主要内容,如果未能解决你的问题,请参考以下文章