ECSHOP商品详情页调用同一分类下产品

本文介绍了一个PHP函数samecat_goods(),用于从数据库中获取指定分类下的商品信息,并通过$smarty模板引擎展示这些商品。该函数接受分类ID等参数,返回包含商品ID、名称、价格等信息的数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.includes\lib_goods.php,添加function samecat_goods
/**
* 获得同分类下的商品
*
* @access public
* @param integer $cat_id 分类ID
* @param integer $num 数量
* @param string $from 来自web/wap的调用
* @param string $order_rule 指定商品排序规则
* @return array
*/
function samecat_goods($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, ' .
		   "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 rand()";
	$sql .= " 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]['goods_name'] = $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;
}
2.goods.php
$properties = get_goods_properties($goods_id);  // 获得商品的规格和属性
后,添加
        $sql= "select cat_id from ".$GLOBALS['ecs']->table('goods')."where goods_id ='".$goods_id."'";
	$thiscat_id = $GLOBALS['db']->getOne($sql);
$smarty->assign('hot_goods',       	   get_recommend_goods('hot'));
后,添加
$smarty->assign('best_goods',      	   samecat_goods($thiscat_id, 3));//3表示调用3条信息
3.goods.dwt文件中适当位置添加
                                    <!--{foreach from=$best_goods item=goods name=pro}-->
                                        <dl class="botNoline">
                                            <dt><a href="{$goods.url}">
                                                <img src="{$goods.thumb}" title="{$goods.short_style_name}"></a></dt>
                                            <dd><a href="{$goods.url}">{$goods.short_style_name}</a></dd>
                                        </dl>
                                    <!--{/foreach}-->

样式自己修改

完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值