【php开源代码栏目提醒】:网学会员,鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“add_to_car.php”一文,供大家参考学习!
<?php
//取得窗体数据
$product_num = $_GET["product_num"];
$product_name = $_GET["product_name"];
$product_price = $_GET["product_price"];
$product_discount = $_GET["product_discount"];
$product_pic = $_GET["product_pic"];
$quantity = $_POST["quantity"];
if (empty($quantity)) $quantity = 1;
//若购物车没有任何项目,则直接加入产品数据
if (empty($_COOKIE["product_num_list"]))
{
setcookie("product_num_list", $product_num);
setcookie("product_name_list", $product_name);
setcookie("price_list", $product_price);
setcookie("discount_list", $product_discount);
setcookie("pic_list", $product_pic);
setcookie("quantity_list", $quantity);
}
else
{
//取得购物车资料
$product_num_array = explode(",", $_COOKIE["product_num_list"]);
$product_name_array = explode(",", $_COOKIE["product_name_list"]);
$price_array = explode(",", $_COOKIE["price_list"]);
$discount_array = explode(",", $_COOKIE["discount_list"]);
$pic_array = explode(",", $_COOKIE["pic_list"]);
$quantity_array = explode(",", $_COOKIE["quantity_list"]);
//判断选择的物品有在购物车中
if (in_array($product_num, $product_num_array))
{
//如果选择的物品已经在购物车中,变更物品数量即可
$key = array_search($product_num, $product_num_array );
$quantity_array[$key] += $quantity;
}
else
{
//如果选择的物品没有在购物车中,则将物品数据加入购物车
$product_num_array[] = $product_num;
$product_name_array[] = $product_name;
$price_array[] = $product_price;
$discount_array[] = $product_discount;
$pic_array[] = $product_pic;
$quantity_array[] = $quantity;
}
//储存购物车数据
setcookie("product_num_list", implode(",", $product_num_array));
setcookie("product_name_list", implode(",", $product_name_array));
setcookie("price_list", implode(",", $price_array));
setcookie("discount_list", implode(",", $discount_array));
setcookie("pic_list", implode(",", $pic_array));
setcookie("quantity_list", implode(",", $quantity_array));
}
?>
<HTML>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
<style type="text/css">
<!--
body {
background-image: url(../picture/b3.gif);
}
-->
</style>
</HEAD>
<P ALIGN="center">您所选取的商品及数量已成功地放入购物车!</P>
<P ALIGN="center"><A HREF="catalog.php">继续购物</A></P>
</BODY>
</
HTML>