<a href="http://localhost/DemoShop/controller/cart.php?cartId='<?php echo $val['id'] ?>'">加入购物车</a>
//前端传值
$cartId=$_GET['cartId'];
include_once"../models/ShopModel.class.php";
$add_cart = new ShopModel();
$addCart=$add_cart->addToCart($cartId);
@session_start();
@$arrayOne=$_SESSION['order'];
/*print_r($addCart);*/
if($arrayOne==false){
//第一次购买
$arrayOne["$cartId"]=array("id"=>$cartId,"name"=>$addCart["name"],"num"=>1);
/*$_SESSION['order']=$arrayOne;*/
}else{
//再次购买
if(array_key_exists("$cartId",$arrayOne)==false){
//1.再次购买 买过的
$arrayOne["$cartId"]=array("id"=>$cartId,"name"=>$addCart["name"],"num"=>1);
}else{
//2.再次购买 没有买过的 只修改数量
$oldNum=$arrayOne["$cartId"]["num"];
$arrayOne["$cartId"]["num"]=$oldNum+1;
}
}
$_SESSION['order']=$arrayOne;
include_once"../view/cartList.php";