Php 未能调整项目金额
Hi am am使用下面的代码检查具有相同pid、长度和类别的物品是否已经在购物篮中,如果已经在购物篮中,则应调整物品的数量。问题是,如果您添加了具有相同pid、长度和类别的项目,而不是数量增加。它作为一种新产品被添加到购物篮中Php 未能调整项目金额,php,pdo,Php,Pdo,Hi am am使用下面的代码检查具有相同pid、长度和类别的物品是否已经在购物篮中,如果已经在购物篮中,则应调整物品的数量。问题是,如果您添加了具有相同pid、长度和类别的项目,而不是数量增加。它作为一种新产品被添加到购物篮中 NOTE: An item has the same pid but it comes in different sizes and category. if (isset($_POST['pid'])) { $pid = $_POST['pi
NOTE: An item has the same pid but it comes in different sizes and category.
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$length = $_POST["size"];
$qty = $_POST['Qty'];
$Category = $_POST['Category'];
$wasFound = false;
$i = 0;
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
} else {
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value, $ilength, $pcategory) = each($each_item)) {
if ($key == "item_id" && $ilength == $length && $pcategory == $Category && $value == $pid) {
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
}
}
}
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
}
}
header("location: cart.php");
exit();
}
?>
注意:项目具有相同的pid,但其大小和类别不同。
如果(isset($_POST['pid'])){
$pid=$_POST['pid'];
$length=$_POST[“size”];
$qty=$_POST['qty'];
$Category=$_POST['Category'];
$wasFound=false;
$i=0;
如果(!isset($_会话[“cart_数组])|计数($_会话[“cart_数组])<1){
$\会话[“购物车数组”]=数组(0=>数组(“项目id=>$pid,“长度”=>$length,“类别”=>$Category,“数量”=>$qty));
}否则{
foreach($\会话[“购物车数组”]作为$each项目){
$i++;
while(列表($key、$value、$ilelength、$pcategory)=每个($each_item)){
如果($key==“item_id”&&$ilength==$length&&$pcategory==$Category&&$value==$pid){
数组拼接($会话[“购物车数组”],$i-1,1,数组(数组(“项目id”=>$pid,“长度”=>$length,“类别”=>$Category,“数量”=>$each_项目['quantity']+1));
$wasFound=true;
}
}
}
如果($wasFound==false){
数组推送($\会话[“购物车数组”],数组(“项目id=>$pid,“长度”=>$length,“类别”=>$Category,“数量”=>$qty));
}
}
标题(“位置:cart.php”);
退出();
}
?>
试试这个
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
} else {
foreach ($_SESSION["cart_array"] as $key=>$each_item) {
if(($each_item['item_id']==$pid && ($each_item['length']==$length)&&($each_item['Category']==$Category)){
$_SESSION["cart_array"][$key]['quantity']++;
$wasFound = true;
$wasFound = true;
}
}
if ($wasFound == false) {
array_splice($_SESSION["cart_array"], array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
}
}
header("location: cart.php");
exit();
}
if(!isset($_SESSION[“cart_array”])| | count($_SESSION[“cart_array”])<1){
$\会话[“购物车数组”]=数组(0=>数组(“项目id=>$pid,“长度”=>$length,“类别”=>$Category,“数量”=>$qty));
}否则{
foreach($\会话[“购物车数组”]作为$key=>$each项目){
如果($each_item['item_id']=$pid&($each_item['length']=$length)&&($each_item['Category']=$Category)){
$\会话[“购物车数组”][$key]['quantity']++;
$wasFound=true;
$wasFound=true;
}
}
如果($wasFound==false){
数组拼接($会话[“购物车数组”],数组(“项目id=>$pid,“长度”=>$length,“类别”=>$Category,“数量”=>$qty));
}
}
标题(“位置:cart.php”);
退出();
}
我想试试;)
更新和测试代码
foreach ($_SESSION["cart_array"] as $array_key=>$each_item) {
if ($each_item['item_id'] == $pid && $each_item['length'] == $length && $each_item['Category'] == $Category) {
$_SESSION["cart_array"][$array_key]['quantity']+=$qty;
$wasFound = true;
}
}
为您的应用程序添加测试条件requirement@DevZer0请问你的意思是什么?不确定数组_splice()应该在那里还是只是一个堆栈溢出misstype@KyleK我用过它。。。我不知道还有什么别的选择。请你之前发布的anser仍然在做同样的事情array\u push仍然以同样的方式工作case@IMSoP你能看一看这个吗?它仍然在添加相同的项目、长度和类别,而不是增加相同的数量。我添加了类别,因为你忽略了这一点,现在它没有显示出来。。。如果我添加相同长度和不同类别的相同项目,它不会显示,而是更新前一个项目