1.includes\lib_goods.php,在最末尾添加几个function
/** * 获得指定分类下的子分类 * * @access public * @param integer $cat_id 分类编号 * @return array */ function get_children_tree($cat_id) { if ($cat_id >0 ) { $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id'"; if ($GLOBALS['db']->getOne($sql)) { // 获取当前分类名及其子类 $sql = 'SELECT a.cat_id, a.cat_name, a.cat_desc, a.sort_order AS parent_order, a.cat_id, ' . 'b.cat_id AS child_id, b.cat_name AS child_name, b.cat_desc AS child_desc, 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 a.cat_id ASC,parent_order 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, a.cat_desc, b.cat_id AS child_id, b.cat_name AS child_name, b.cat_desc AS child_desc, 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']); $cat_arr[$row['cat_id']]['cat_desc'] = $row['cat_desc']; 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']); $cat_arr[$row['cat_id']]['children'][$row['child_id']]['cat_desc'] = $row['child_desc']; } } return $cat_arr; } } /** * 获取指定分类产品 * * @access public * @param string $cat_id(num)_best_goods * @param array $cat_id(num)_best_goods * @return array */ function get_cat_id_goods_list($cat_id = '', $num = '') { $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, g.sales_volume_base, g.is_shipping, ' . "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))))) .")"; $sql .= "ORDER BY g.sales_volume_base DESC LIMIT $num"; $res = $GLOBALS['db']->getAll($sql); $goods = array(); foreach ($res AS $idx => $row) { $goods[$idx]['id'] = $row['goods_id']; $goods[$idx]['name'] = $row['goods_name']; $goods[$idx]['brief'] = $row['goods_brief']; $goods[$idx]['sales_volume_base'] = $row['sales_volume_base']; $goods[$idx]['is_shipping'] = $row['is_shipping']; $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]['promote_price'] = price_format($row['promote_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; } /** * 获取指定分类产品的相册 * * @access public * @param string $cat_idnum_best_goods * @param array $cat_idnum_best_goods * @return array */ function get_thumb($goods_id){ $sql = "SELECT img_url,thumb_url FROM " . $GLOBALS['ecs']->table('goods_gallery') ." WHERE goods_id = ".$goods_id; $res = $GLOBALS['db']->getAll($sql); return $res; }
2.category.php,在
$smarty->assign('feed_url', ($_CFG['rewrite'] == 1) ? "feed-c$cat_id.xml" : 'feed.php?cat=' . $cat_id); // RSS URL
后面,添加
/********************************************************************************************/ //在分类页面调用当前分类以及其二级分类的信心,再在每个分类下调用5个商品 $cats = get_children_tree($cat_id);//获取当前分类信息以及子分类信息 $cat_detail = array(); foreach ($cats as $key => $val) { foreach($cats[$key]['children'] as $_k =>$_v){ $cats[$key]['children'][$_k]['goods_detail'] = get_cat_id_goods_list($_v['id'],5); foreach($cats[$key]['children'][$_k]['goods_detail'] as $_ke =>$_va){ $cats[$key]['children'][$_k]['goods_detail'][$_ke]['thumb_url'] = get_thumb($_va['id']); } } $cat_detail = $cats[$key]['children'];//当前分类下的二级分类及其商品信息 } $smarty->assign('cat_detail', $cat_detail); /********************************************************************************************/
<!--循环二级分类--> <!--{foreach from=$cat_detail item=cat name=cats_detail}--> <div id=content class="w pt10"> <div class="lzo"> <div class="lzt"> <h2 class="left f16 pl10"><a href="{$cat.url}" target="_blank">{$cat.name}</a></h2> <a class="right gray pr10" href="{$cat.url}" target="_blank">查看更多>></a> </div> <div class="left lzr over"> <div class="w10 left"> <!--{foreach from=$cat.goods_detail item=goods name=goods_detail}--> <dl> <dt><a href="{$goods.url}" target="_blank" title="{$goods.name|escape:html}"><img class="thm" width="220" height="220" src2="{$goods.thumb}"></a></dt> <dd class="thumb"> <!--{foreach from=$goods.thumb_url item=url name=urls}--> <img width="30" height="30" src2="{$url.thumb_url}" data-s="{$url.img_url}"> <!--{/foreach}--> </dd> <dd class="g"><b class="xj"><!-- {if $goods.promote_price neq "¥0.00元"} -->{$goods.promote_price}<!-- {else}-->{$goods.shop_price}<!--{/if}--></b><span class="yj">{$goods.market_price}</span></dd> <dd class="ms fr"><a title="{$goods.name|escape:html}" class="c3 fr" href="{$goods.url}" target="_blank">{$goods.short_name|escape:html}</a> <p title="{$goods.brief|escape:html}" class="gray">{$goods.brief|escape:html}</p> </dd> <dd class="sa"><span>销售:<em>{$goods.sales_volume_base|escape:html}</em></span><!--<span><a href="#" target="_blank">评论:<b>23</b></a></span><a onclick="adCar(this,5503)" title="加入购物车" class="lcar" href="javascript:;"></a>--></dd> </dl> <!--{/foreach}--> </div> </div> <div class="clear"></div> <H3 class="gray fN fr">{$cat.cat_desc}</H3> </div> <p class=lh16></p> </div> <!--{/foreach}--> <!--循环二级分类-->
如图所示:
ok!完成,顺带说一下,这个循环分类以及显示分类下商品的功能之前博主我在网上找了很久都没有找到,遂,索性自己开发。最关键的是category.php里面的那个foreach循环,有兴趣的同学可以研究看看。特此分享给给位博友。
若要转载请注明出处:http://blog.csdn.net/glatchen/article/details/42238499
时间: 2024-11-08 01:15:07