PHP.50-TP框架商城应用实例-前台2-商品推荐

商品推荐

{抢购,新品,精品,热卖}效果如下

 

1、在商品表增加促销价格促销起始时间结束时间三个字段,字段推荐楼层排序在后面用到

 

 注意:在增加datetime类型字段时,要添加默认值{datetime范围:1000-01-01 00:00:00 到 9999-12-31 23:59:59};并且关闭MySQL严格模式,因为promote_price(decimal(10,2))无法插入空值;但建议开发阶段开启

2、表单与时间插件

3、修改商品模型允许接收字段

4、前台取出推荐的商品

  // 取出当前正在促销的商品
        public function getPromoteGoods($limit = 5)
        {
            $today = date('Y-m-d H:i');
            return $this->field('id, goods_name, mid_logo, promote_price')
                ->where(array(
                'is_on_sale' => array('eq', '1'),
                'promote_price' => array('gt', 0),
                'promote_start_date' => array('elt', $today),
                'promote_end_date' => array('egt', $today),
            ))->limit($limit)
            ->select();
        }
    // 取出三种推荐{热卖、精品、新品}
        public function getRecGoods($recType)
        {
            return $this->field('id,goods_name,mid_logo,shop_price')
            ->where(array(
                'is_on_sale' => array('eq', '1'),
                "$recType" => array('eq', '是')
            ))
            ->limit($limit)
            ->order('sort_num')   // 根据sort_num字段排序
            ->select();
        }

5、在Home/IndexController.class.php控制器中取出

6、index.html页面循环输出

注:为了能够更精确的排序商品,我们可以为商品再添加一个字段sort_num,这个字段保存一个数字,数字越小越靠前

 

 

 

 

 

 

posted on 2017-07-09 08:48  子轩非鱼  阅读(655)  评论(1编辑  收藏  举报

导航