I am making an e-commerce website and I have run into an issue with updating item quantity. When I go back to the same page of an item already in my cart and put in a new quantity, I return to my cart page and all of the items now have the same quantity as the item last updated.
if (!isset($_SESSION['shoppingcart'])) {
$_SESSION['shoppingcart'] = array();
$_SESSION['name'] = array();
$_SESSION['quantity'] = array();
$_SESSION['price'] = array();
}
if (in_array($_POST['id'], $_SESSION['shoppingcart'])) {
for ($i = 0; $i < sizeof($_SESSION['quantity']); $i ) {
$_SESSION['quantity'][$i] = $_POST['quantity']; /*<-- I am thinking it is this piece right here.*/
removeItemsZero($i);
$added = '<p >Updated cart! Click <a href="catalog.php">here</a> to continue shopping!</p>';
}
} else {
$_SESSION['shoppingcart'][] = $_POST['id'];
$_SESSION['quantity'][] = $_POST['quantity'];
$_SESSION['name'][] = $name;
$_SESSION['price'][] = $price;
$added = '<p >Product added! Click <a href="catalog.php">here</a> to continue shopping!</p>';
}
I am assuming it has something to do with $_SESSION['quantity'][$i] = $_POST['quantity']; piece, but I am unsure of any other ways to update the quantity on the product description page.
EDIT
I changed some things around, but now regardless of whichever product from the product page I select, I can't seem to change the quantity of that item specifically, but that value I enter into the field gets applied to the first index of the quantity array.
if (in_array($_POST['id'], $_SESSION['shoppingcart'])) {
$key = array_search($_POST['id'], $_SESSION['quantity']);
$_SESSION['quantity'][$key] = $_POST['quantity'];
for ($i = 0; $i < sizeof($_SESSION['quantity']); $i ) {
removeItemsZero($i);
$added = '<p >Updated cart! Click <a href="catalog.php">here</a> to continue shopping!</p>';
}
EDIT2
This is the code for the cart page with checkout
$cartTable = '<table >';
$tableHeader = '<tr><th>NAME</th><th>QUANTITY</th><th>PRICE</th><th>TOTAL</th></tr>';
$endTable = '</table>';
$total = 0;
$totalDiscounted = 0;
$emptyCart = '';
$msg = '';
if (!empty($_SESSION['shoppingcart'])) {
$conn = mysqli_connect(HOST, USER, PASS, DB);
if (isset($_POST['update'])) {
for ($i = 0; $i < sizeof($_SESSION['quantity']); $i ) {
$postID = 'qty' . $i;
$_SESSION['quantity'][$i] = $_POST[$postID];
removeItemsZero($i);
}
}
}
if (isset($_POST['purchase'])) {
$msg = '<p>Thanks for purchasing! This is what you ordered</p><br>';
for ($i = 0; $i < sizeof($_SESSION['shoppingcart']); $i ) {
$list .= '<div ><p >Order List</p><ul><li>' . $_SESSION['quantity'][$i] . ' ' . $_SESSION['name'][$i] . '</li></ul></div>';
}
unset($_SESSION['shoppingcart']);
}
if (isset($_POST['clear'])) {
unset($_SESSION['shoppingcart']);
$emptyCart = '<p>Your cart is empty!</p>';
} else if(isset($_POST['purchase'])){
$emptyCart = '<p>Your cart is now empty!</p>';
}else
{
$emptyCart = '<p>Your cart is empty!</p>';
}
?>
<!doctype html>
<html lang="en">
<head>
<script src="js/script.js"></script>
<link rel="stylesheet" href="css/style.css" type="text/css">
<title>Acme Online Store - cart</title>
</head>
<body>
<nav>
<div ><h2>Acme Online Store</h2></div>
<ul>
<li><a href="catalog.php">Products</a></li>
<li><a href="cart.php">Cart</a></li>
<li><a href="logout.php">Log Out</a></li>
</ul>
</nav>
<div >
<form method="post">
<?php
if (!empty($_SESSION['shoppingcart'])) {
for ($i = 0; $i < sizeof($_SESSION['shoppingcart']); $i ) {
$cart .= '<tr>';
$cart .= '<td>' . $_SESSION['name'][$i] . '</td>';
$cart .= '<td><input type="text" id="quantity"name="qty' . $i . '"value="' . $_SESSION['quantity'][$i] . '" onkeyup="checkInput()"></td>';
$cart .= '<td>$' . $_SESSION['price'][$i] . '</td>';
$cart .= '<td>$' . $_SESSION['quantity'][$i] * $_SESSION['price'][$i] . '</td>';
$cart .= '</tr>';
$total = ($_SESSION['quantity'][$i] * $_SESSION['price'][$i]);
$totalDiscounted = ($_SESSION['quantity'][$i] * $_SESSION['price'][$i]) / 100 * 30;
}
echo $cartTable;
echo $tableHeader;
echo $cart;
echo $endTable;
echo '<br>';
echo '<p>All orders are 30% off for the holidays!</p><br>';
echo '<table >';
echo '<tr>';
echo '<th>Total</th><th>Total After Discount</th><th>Savings</th></tr>';
echo '<tr><td> $' . $total . '</td>';
echo '<td>$' . number_format((float)$total - $totalDiscounted, 2, '.', '') . '</td>';
echo '<td>$' . number_format((float)$totalDiscounted, 2, '.', '') . '</td>';
echo '</tr>';
echo '</table>';
?>
CodePudding user response:
It would be better to include a self-contained example, but here is one method. We are saving the ID as the key to an array. That is, array(1 => array(quantity = 10));
would be the data structure of the items within our shopping cart. This approach means we are determining whether the key exists and then updating shopping cart array.
<?php
session_start();
if (!isset($_SESSION['shoppingcart'])) {
$_SESSION['shoppingcart'] = array();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$postId = cleanInput($_POST["id"]);
$postQuantity = cleanInput($_POST['quantity']);
if (!empty($_SESSION['shoppingcart'][$postId])) {
$_SESSION['shoppingcart'][$postId]['quantity'] = $postQuantity;
// you can add code that you updated existing record
} else {
$_SESSION['shoppingcart'][$postId] = array("quantity" => $postQuantity);
// you can add code that you added a new record
}
}
function cleanInput(string $data) : string
{
$data = trim($data); // removes whitespace
$data = stripslashes($data); // strips slashes
$data = htmlspecialchars($data); // replaces html chars
return $data;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>My Shopping Cart</title>
</head>
<body>
<form method = "post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for = "id" name = "id">Enter Id</input><br>
<input type="number" for = "id" name = "id" required><br>
<label for = "quantity" name = "quantity">Enter Quantity</input><br>
<input type="number" for = "quantity" name = "quantity" required><br>
<input type="submit">
</form>
<?php
if (count($_SESSION['shoppingcart']) == 0) {
echo "<h2>No orders entered!</h2>";
} else {
echo "<table>
<thead>
<tr>
<th>id</th>
<th>quantity</th>
</tr>
</thead>
<tbody>";
foreach($_SESSION['shoppingcart'] as $id =>$shoppingItem) {
$quantityRes = $shoppingItem['quantity'];
echo "<tr><td>$id</td><td>$quantityRes</td></tr>";
}
echo " </tbody></table>";
}
?>
</body>
<html>