'calculate quantity from shopping basket ($_SESSION['basket'];)
I'm trying to create a shopping cart for the past few months and this has always been a problem that I just can't get solved. I have looked up different shopping carts online that are session based. tried what they did but this only results to having only 1 item updating every time. I have asked my friends if they see what is going wrong and they can't find it either. So does anyone know how to update quantity along side the item that the user wants to update from the cart??
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errrors', '1');
// session_destroy();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Cart</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<link href="css/stylecart.css" rel="stylesheet" />
<script src="js/scripts.js" async></script>
</head>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
}
a{
text-decoration: none;
color: white;
}
</style>
<body>
<!--navbar-->
<a class="back" href="index.php"> <i class="bi bi-arrow-left-circle-fill bi-5x"></i></a>
<?php
include "config.php";
?>
<div class="text-center" style="font-size: 100px;">🛍</div>
<h2 class="text-center">Winkelmandje</h2><br>
<section class="container content-section">
<!-- <h2 class="section-header">CART</h2> -->
<div class="cart-row">
<span class="cart-item cart-header cart-column">ITEM</span>
<span class="cart-item cart-header cart-column">PRICE</span>
<span class="cart-item cart-header cart-column">QUANTITY</span>
<!-- <span class="cart-item cart-header cart-column">berekening</span> -->
<!-- <span class="cart-item cart-header cart-column">Verwijderen</span> -->
</div>
<?php
if (isset($_SESSION['basket'])){
if( in_array( $_GET['broodjes_ID'] ,$_SESSION['basket']) )
{
}else{
$_SESSION['basket'][] = $_GET['broodjes_ID'];
}
}else{
$_SESSION['basket'][]= $_GET['broodjes_ID'];
}
$sumtotal = 0;
foreach($_SESSION['basket'] as $key => $value){
//print_r(array_keys($_SESSION['basket']));
//echo "Key = $key; value = $value; <br>";
$sql = "SELECT * FROM broodjes WHERE broodjes_ID=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $value);
$stmt->execute();
$result = $stmt->get_result();
if($row = $result->fetch_assoc()){
echo '<div class="cart-items">';
echo '<div class="cart-row">';
echo '<div class="cart-item cart-column">';
echo $row['broodnaam'];
echo '</div>';
echo '<div class="cart-item cart-column">';
echo '€ ' . $row['prijs'];
echo '</div>';
//quantity
echo '<div class="cart-item cart-column">';
echo '<form action="" method="POST">';
echo '<div class="col-xs-4">';
echo '<input type="hidden" name="broodnaam" id="broodnaam" value="' . $row['broodnaam'] . '">';
echo '<input type="number" name="quantity" id="quantity" class="form-control input-sm" value="1" min="1" max="'.$row['voorraad'].'">';
echo '</div>';
echo '</form>';
echo '</div>';
$_SESSION['quantity'] = $_POST['quantity'];
if ($_POST['broodnaam'] == $_POST['quantity']){
$rowtotaal = $row['prijs'] * $_SESSION['quantity'];
$sumtotal += $rowtotaal;
}else{
$rowtotaal = $row['prijs'] * 1;
$sumtotal += $rowtotaal;
}
echo '</div>';
echo '</div>';
}
}
?> <br />
<div class="cart-total">
<strong class="cart-total-title">Total</strong>
<span class="cart-total-price"> € <?php echo $sumtotal;?></span>
</div>
<br/>
<button type="button" class="btn btn-danger"><a href="bestellen.php"> bestellen</a></button>
<button type="button" class="btn btn-danger" name="delete"><a href="deleteeverything.php"> empty cart</a></button>
<br>
<br />
</section>
</body>
mysqli table broodjes (maybe this helps)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

