I am trying to update a particular session variable in an array.
How i set up the session
$_SESSION['cart_items'][] = [
'size' => $size,
'color' => $color,
'qty' => $quantity,
'price' => $price,
'productId' => $productId,
'image' => $image,
'name' => $name,
];
I want to update the ‘qty’ when i press update in the form.
foreach ($_SESSION["cart_items"] as $key => $item) {
$item_price = $item["qty"] * $item["price"];
$total_quantity += $item["qty"];
$item_total = $item_price * $total_quantity;
$total_price += ($item["price"] * $item["qty"]);
$_SESSION['totalprice'] = $total_price;
//html code
echo '
<div class="cartitem">
<div id="cartimage">
<img src=' . $item['image'] . '>
</div>
<div id="cartdesc">
<form method="get" action="cart.php">
<p id="cartitemname"> ' . $item["name"] . ' </p>
<p>Quantity: <input id="updateprice" name="updateprice" type="number" step="1" min="1" value="' . $item["qty"] . '"> </p>
<p>Size: ' . $item["size"] . ' </p>
<p>Price:$ ' . $item["price"] . ' </p>
<p>Item total price $ ' . number_format($item["qty"] * $item["price"], 2) . ' </p>
<button type="submit" name="update">Update</button>
<a href="cart.php?action=remove&code=' . $key . '" class="btnRemoveAction"><img id="deletebtn" src="res/istockphoto-928418914-170667a.jpg" alt="Remove Item" /></a>
</form>
</div>
</div>
';
}
the get method called when i press update :
if (isset($_GET['update'])) {
//print_r($_SESSION["cart_items"]);
// what i have tried :
// $_SESSION['cart_items']['qty'] = $_GET['updateprice']
//$item["qty"] = $_GET['updateprice'];
header('location:cart.php');
}
Both of these don’t work! any help or nudge in the right direction would be greatly appreciated!
2
Answers
Solution!! Building ontop of M. Eriksson advice
Ideally, one should have a unique Identifier in the database. Use it and replace the 'uniquekey' i wrote here. I do not have a unique key, thus the following method.
At the html form side,
Using JS to generate a unique key:
Added an additional identifier, unique key
Now, you are able to identify the specific entry in the array!
The issue is with how you add items to the cart. You’re just pushing them into a indexed array, which makes it hard to reference a specific cart item after it’s added.
Demo:
To be able to update any of them, you would need to do:
This makes it harder since you would need to know the index the cart item was added with. Unless you want to iterate through the cart and check each item if it is the one you want to change.
A solution would be to use an associative array instead, where you set the array key:
Now you can access it specifically using the identifier
The identifier should probably be a unique identifier for that specific variation of the product, like size, color etc. since you could have different cart items for the same product, but with different variations.
In e-commerce systems, each variation, (or Stock Keeping Unit (SKU)), usually has a unique identifier, which would be a good identifier. Exactly how you should set them up depends on your application. It gets a bit more complicated if you consider if you should be able to change the size, color etc on an existing one. You would then need to remove the old cart item for the previous variation and add the new.
Hope it clarifies things a bit.