Click here to Skip to main content
15,446,510 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?
PHP
<pre><?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/bootstrap-icons@1.5.0/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"> </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">
                ITEM
                PRICE
                <!-- QUANTITY  -->
            </div>  
            <?php 
            $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); 
                         $stmt->execute();
                         $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;?>
                    </div>
                    <br/>
        
                <button type="button" class="btn btn-danger"><a href="emptyarray.php">Empty array</a></button>
            
             <br>
           <br />
        </section>
                </body>



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
Posted
Updated 17-Jan-22 8:02am

Move the $sum = 0 line outside of the foreach loop. You're resetting it for each item in your basket.
 
Share this answer
 
No not
$sum =+ $row['prijs'];

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


And did you learn something from cart1.php I provided earlier? You should try and study it IMHO.
 
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