Click here to Skip to main content
15,941,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I'm so sorry but hwo do I calculate the cart total? right now for some reason it only seems to show the last price at the total price how do I change this? I want it to count everything from the cart?
ini_set('display_errrors', '1');
// session_destroy();
<!DOCTYPE html>
<html lang="en">
        <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="" />
        <!-- Favicon-->
        <link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
        <!-- Bootstrap icons-->
        <link href="" 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>

    body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 20px;

        text-decoration: none;
        color: white;

<a class="back" href="index.php"> </a>
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">
                <!-- QUANTITY  -->
            $broodjes = $_GET['broodjes_ID'];
            $_SESSION['basket'][] = $broodjes;
                     foreach($_SESSION['basket'] as $key => $value){
                         //echo "Key = $key; value = $value; <br>";
                         $sql = "SELECT broodjes_ID, broodnaam, prijs FROM broodjes WHERE broodjes_ID=$value";
                         $stmt = $conn->prepare($sql); 
                         $result = $stmt->get_result();
                         $sum = 0;
                         while($row = mysqli_fetch_assoc($result)){
                            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>";
                            echo "</div>";
                            echo "</div>";
                            $sum =+ $row['prijs'];
                     ?> <br />

                         <!--     } -->
                         <!--  } -->
                    <div class="cart-total">
                        Total<?php   echo $sum;?>
                <button type="button" class="btn btn-danger"><a href="emptyarray.php">Empty array</a></button>
           <br />

What I have tried:

I tried looking at around on stack over flow but they just tell me do $sum = 0; before the while loop and then at the end of the while loop do $sum += $row['price']; but this doesn't work it shows nothing in the total part
Updated 17-Jan-22 8:02am

No not
$sum =+ $row['prijs'];

$sum += $row['prijs'];

And did you learn something from cart1.php I provided earlier? You should try and study it IMHO.
Share this answer
Move the $sum = 0 line outside of the foreach loop. You're resetting it for each item in your basket.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900