商城购物车

2017-07-31  本文已影响0人  YSail

<?php

namespaceHome\Controller;

useThink\Controller;

header("content-type:text/html;charset=utf8");

classShopCarControllerextendsIndexController{//继承首页类

public function_initialize(){//初始化调用首页方法 获得菜单栏需要的值

$categoryinfo=$this->catrgoryName();

$this->assign('categoryinfo',$categoryinfo);//分类名称赋值

$cartTop=$this->cartTop();

$allNum=count($cartTop);

$this->assign('allNum',$allNum);

$this->assign('cartTop',$cartTop);

}

//加入购物车

public functionmyShopcar(){

$mid=session()['mid'];

$pid=$_POST['pro_id'];

$detail_id=$_POST['products_detail_id'];

if($mid>0) {

//查询购物车表是否已经存在该商品

$res=M('cartlist')->field('cart_id,cart_buynum')->where("cart_mid=$midand cart_pid=$pidand cart_detail_id=$detail_id")->find();

$cid=$res['cart_id'];

$buynum=$res['cart_buynum'];

if($cid) {//如果该用户已经购买过此商品则更新数量更新时间

$newCart['cart_id']=$cid;

$newCart['cart_buynum']=$buynum+$_POST['buynum'];

$newCart['cart_addtime']=time();

M('cartlist')->save($newCart);

}else{//如果该用户未购买商品则加入购物车

$cart['cart_pid']=$pid;

$cart['cart_mid']=$mid;

$cart['cart_buynum']=$_POST['buynum'];

$cart['cart_addtime']=time();

$cart['cart_detail_id']=$detail_id;

M('cartlist')->add($cart);

}

$result=1;

echojson_encode($result);//转换json格式

exit;

}else{

$result=0;

echojson_encode($result);//转换json格式

exit;

}

}

//购物车

public functioncartList(){

$mid=session()['mid'];

if($mid>0){

//联合两个表查询购物车里的数据

$model=M();

$goods=$model

->table('echop_cartlist cartlist,ecshop_products products')

->where("cartlist.cart_pid=products.pro_id and cartlist.cart_mid=$mid")

->field('products.pro_id,products.pro_img,products.pro_name,products.pro_collect,cartlist.cart_buynum,cartlist.cart_detail_id')

->order("cartlist.cart_addtime desc")

->select();

foreach($goodsas$k=>$val){

$detail_id=$val['cart_detail_id'];

$detail=getDetails($detail_id);

$goods[$k]['type']=$detail['type'];

$goods[$k]['size']=$detail['size'];

$goods[$k]['price']=$detail['price'];

}

}

}

}

上一篇下一篇

猜你喜欢

热点阅读