Home > Back-end >  Increase quantity value by 1 in PHP session
Increase quantity value by 1 in PHP session

Time:05-30

I will a PHP code that checks the session if an ID of an item exists and it does not exist it should add the item. I need to increase the quantity of an item that ID exist. For example when you add an item to a cart and you added the item initially, instead of duplicating the item it should just increase the quantity. Below is my code and I will like the increase in quantity to happen where we have if.

$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.

CodePudding user response:

Change $quantity 1 into $quantity= $quantity 1

CodePudding user response:

You can use $quantity instead of $quantity 1

CodePudding user response:

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.

  •  Tags:  
  • php
  • Related