<?php
header("Content-type: text/html; charset=utf-8");
session_start();//启动session
// session_destroy();
// die();
// var_dump($_SESSION);
// die();
if(isset($_SESSION['username']) &&!empty($_SESSION['username'])){
}
else{
echo '<script language="JavaScript">;
alert("尚未登录,请先登录!!");</script>';
echo "<script>location.href='header.php'</script>"; exit;
}
// $uid = $_SESSION['uid'];
$goods_id = $_POST['goods_id'];//获取商品id
// $size = $_POST['size'];
// $color = $_POST['color'];
$number = $_POST['num'];//获取商品数量 // $numcol="$size"."$color"; // var_dump($_POST);
// die();
if(empty($_SESSION["gwc"])) { //1.购物车是空的,第一次点击添加购物车 $arr[$goods_id] =array(
'goods_id' => $goods_id,
'num' => $number
); $_SESSION["gwc"]=$arr; // print_r($arr);
// die();
} else { //不是第一次点击 //判断购物车中是否存在该商品 $arr = $_SESSION["gwc"]; //先存一下 $chuxian = false; //查询购物车中是否有该商品,KEY是商品id
foreach($arr as $k=> $v)
{
if($k==$goods_id)
{ $chuxian = true; } }
// 购物车中已有该商品,则只需将它的数量增加
if($chuxian)
{ $arr[$goods_id]['num']+=$number; //3.如果购物车中有该商品
// if(!isset($arr[$goods_id])){
// $arr[$goods_id]=array(
// 'id' => $goods_id,
// 'num' => $number
// );
// }else{
// $arr[$goods_id]['num']+=$number;
// }
$_SESSION["gwc"] = $arr;
}
else
{ //2.如果购物车中没有该商品
$arr[$goods_id] = array(
'goods_id' => $goods_id,
'num' => $number
);
$_SESSION["gwc"] = $arr;
// print_r($arr);
// die();
} } // echo "OK";
echo '<script language="JavaScript">;
alert("添加成功!");location.href="header.php";;';
exit;
// var_dump($_SESSION); // die ( );
#
?>