PHP会话数组-存储具有不同值的相同项目
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP会话数组-存储具有不同值的相同项目,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3363字,纯文字阅读大概需要5分钟。
内容图文
![PHP会话数组-存储具有不同值的相同项目](/upload/InfoBanner/zyjiaocheng/681/11243cdeda6d4bd3b8107454ebb18aa2.jpg)
我正在使用PHP购物车系统,但遇到了问题.
问题:
当用户添加一个项目,然后再次添加时,添加同一项目但具有不同的值(例如(不同的大小或数量)),购物车会使用用户选择的新值来更新该条目.先前的详细信息将被删除.
我一直在寻找的解决方案
如果用户添加了任何商品,然后又想添加相同的商品但有不同的要求,则应将其添加为购物车会话中的单独条目.(仅当特定变量发生更改时,例如:单个商品但大小不同).
如何在当前代码中执行此操作?
购物车
//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
$product_size = filter_var($_POST["product_size"], FILTER_SANITIZE_NUMBER_INT); //product size
$product_qty = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product quantity
$return_url = base64_decode($_POST["return_url"]); //return url
//MySqli query - get details of item from db using product code
$results = $connection->query("SELECT * FROM products WHERE prod_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
if ($results) { //we have the product info
//prepare array for the session variable
$new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$obj->price));
if(isset($_SESSION["products"])) //if we have the session
{
$found = false; //set found item to false
foreach ($_SESSION["products"] as $cart_itm) //loop through session array
{
if($cart_itm["code"] == $product_code){ //the item exist in array
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$cart_itm["price"]);
$found = true;
}else{
//item doesn't exist in the list, just retrieve old info and prepare array for session var
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$cart_itm["size"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
}
if($found == false) //we didn't find item in array
{
//add new user item in array
$_SESSION["products"] = array_merge($product, $new_product);
}else{
//found user item in array list, and increased the quantity
$_SESSION["products"] = $product;
}
}else{
//create a new session var if does not exist
$_SESSION["products"] = $new_product;
}
}
//redirect back to original page
header('Location:'.$return_url);
}
期待一些有用的答案.
谢谢
*****更新****
从购物车中删除产品的代码:
//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
$product_code = $_GET["removep"]; //get the product code to remove
$return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
{
//if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
if($cart_itm["code"] != $product_code && $cart_itm["size"] != $product_size){
$product[] = array(
'name'=>$cart_itm["name"],
'code'=>$cart_itm["code"],
'size'=>$cart_itm["size"],
'qty'=>$cart_itm["qty"],
'price'=>$cart_itm["price"]
);
}
//create a new product list for cart
$_SESSION["products"] = $product;
}
//redirect back to original page
header('Location:'.$return_url);
}
解决方法:
您使用的if条件是您所做的错误.试试这个
if($cart_itm["code"] == $product_code && $cart_itm["size"] == $product_size)
代替
if($cart_itm["code"] == $product_code)
数量检查的方法不是一个好习惯,因为您可以在现有条目中单独编辑数量.
内容总结
以上是互联网集市为您收集整理的PHP会话数组-存储具有不同值的相同项目全部内容,希望文章能够帮你解决PHP会话数组-存储具有不同值的相同项目所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。