首页 > 代码库 > ecshop首页调用指定分类下面的精品商品

ecshop首页调用指定分类下面的精品商品

1.首先在index.php页面加上这段代码:


function index_get_cat_id_goods_best_list($cat_id, $num)
{
    $sql = ‘Select g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price, g.promote_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 ‘ .
            "Where g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.is_best = 1 AND g.cat_id = ‘$cat_id‘".
            " 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;
}



2.然后声明用$smarty调用
在$smarty->assign(‘shop_notice‘,     $_CFG[‘shop_notice‘]);       // 商店公告      的 下面写上:

$smarty->assign(‘cat_id2_best_goods‘, index_get_cat_id_goods_best_list(2,4));
//2指分类id,4指循环次数

 

 

3.打开index.dwt,调用即可

 

 

<!--{foreach from=$cat_id2_best_goods item=goods}-->
     <div class="goodsItem" style="float:left ; margin-left:10px;" >
           <a href="http://www.mamicode.com/{$goods.url}"><img src="http://www.mamicode.com/{$goods.thumb}" alt="{$goods.name|escape:html}" class="goodsimg" width="100" height="100" /></a><br />
           <p><a href="http://www.mamicode.com/{$goods.url}" title="{$goods.name|escape:html}">{$goods.short_name|truncate:7:true}</a></p>
           <div class="shop_s" style="text-align:center; color:#CC0000 " >
             <strong>
            {$goods.shop_price}
            </strong>
           </div>
     </div>
<!--{/foreach}-->