简体   繁体   中英

Increase quantity value by 1 in PHP session

I wrote a PHP code that checks the session, whether the ID of an item exists. If it does not exist an item should be added to the session. If it exists I need to increase the quantity of an item. For example when you add an item to a cart and If it already exists, Instead of duplicating the item It should just increase the quantity. Below is my code and I would like the increase the quantity in the if condition of the code.

$cart = array (
    'title' => $_POST['title'],
    'price' => $_POST['price'],
    'img_src' => $_POST['img_src'],
    'id' => $_POST['id'],
   'quantity' => $_POST['quantity'],
   
    );
$id = array();
$quantity = array();
foreach ($_SESSION['cart'] as $item) {
   $id[] = $item['id'];
 $quantity[] = $item['quantity'];

}

if(in_array($_POST['id'], $id)){
    
   $quantity+1;
    
}else{
    $_SESSION['cart'][] = $cart;
        $count = count($_SESSION["cart"]);

}

As you can see above I tried $quantity+1; but didn't work.

What is the problem ?

You have a couple of small problems in your code.

I think you will only increment the cart item X if the user push a new cart with the id X to the server. Then the structure could look something like this. But I don't know your entire application, but the code should show you how it works, at least in the simplest case.

The logic can be look:

<?php

$_SESSION['cart'][] = [
    "id" => 1,
    "price" => 100,
    "quantity" => 2,
    "sum" => 200
];

$_POST["newItem"] = [
    "id" => 1,
];

$cart = $_SESSION['cart']; 

foreach($cart as $key => $ci) {
    if ($ci['id'] === $_POST["newItem"]['id']) {
        $cart[$key]["sum"] = ++$cart[$key]["quantity"] * $cart[$key]["price"];
         // if you quantity > 1
        // $cart[$key]["quantity"] = ($cart[$key]["quantity"] + $ci["quantity"]);
        // $cart[$key]["sum"] = $cart[$key]["quantity"] * $cart[$key]["price"];
    }
}

$_SESSION['cart'] = $cart;

The code above works if you don't send the quantty and it is assumed that it is always incremented by one, but if you send the quantity with the post request and it can be more than one you need to replace the following in the code....

$cart[$key]["quantity"] = ($cart[$key]["quantity"] + $ci["quantity"]);
$cart[$key]["sum"] = $cart[$key]["quantity"] * $cart[$key]["price"];

I didn't run your code but from the first impressions, the $quantity+1 only does the calculation and doesn't assign that value to anything. $quantity = $quantity+1 will work if the "if" statement works. Also, there are more incrementing methods you can do.

Your $quantity+1; does not increase the value of your $quantity and assigns the +1 to $quantity variable. You need to change that line to:

$quantity = $quantity+1;

Or

$quantity+=1;

Or

$quantity++;

You can increase the quantity in cart like below,

$cart = array (
    'title' => $_POST['title'],
    'price' => $_POST['price'],
    'img_src' => $_POST['img_src'],
    'id' => $_POST['id'],
   'quantity' => $_POST['quantity'],
   
    );
    $itemcount = 0;
    foreach ($_SESSION['cart'] as $item) {
      if($item['id'] == $_POST['id']) {
        $item['quantity'] += 1;
        $itemcount += 1;
      }
    }
if($itemcount == 0) {
  array_push($_SESSION['cart'],$cart);
}

Here, The first thing I am doing is, I am checking whether the item is already available in $_SESSION['cart'] in foreach loop. If yes, I will be incrementing the quantity by 1. If no, I will be pushing the entire $cart array into $_SESSION['cart'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM