Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP calculate total price based on quantity

220061

Well-Known Coder
can I calculate the total price depending on the number that the user types into the input?
so for example
1642504181342.png
idk how to do this? this is my code. I have been looking around on the internet but they only gave me examples of how their shopping cart was looking. I don't want that I want ot be able to understand how to do this with my shopping cart code.
PHP:
<?php
include "config.php";
        ?>
        <div class="text-center" style="font-size: 100px;">&#128717;</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-price cart-header cart-column">PRICE</span>
                <span class="cart-quantity cart-header cart-column">QUANTITY</span> 
            </div> 
            <?php
            $broodjes = $_GET['broodjes_ID'];
            
            if (isset($_SESSION['basket'])){
                if( in_array( $broodjes ,$_SESSION['basket']) )
                {
                    
                }else{
                    $_SESSION['basket'][] = $broodjes;
                    
                }
            }else{
                $_SESSION['basket'][]= $broodjes;
                
            }

            $sum = 0;
                     foreach($_SESSION['basket'] as $key => $value){

                         //echo "Key = $key; value = $value; <br>";
                         $sql = "SELECT broodjes_ID, broodnaam, prijs, voorraad FROM broodjes WHERE broodjes_ID=$value";
                         $stmt = $conn->prepare($sql);
                         $stmt->execute();
                         $result = $stmt->get_result();
                        
                         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 class="col-xs-4">';
                            echo '<input type="text" class="form-control input-sm" value="1">';
                            echo '</div>';
                            echo '</div>';
                            echo '</div>';
                            //$sum = $row['prijs'] *
                            $sum += $row['prijs'];
                            
                         }
                        
                        
                     }
                     ?> <br />
              
                    <div class="cart-total">
                        <strong class="cart-total-title">Total</strong>
                        <span class="cart-total-price"> € <?php   echo $sum;?></span>
                    </div>
                    <br/>
                    
                
                    <button type="button" class="btn btn-danger"><a href="emptyarray.php"> Bestellen</a></button>
                  
            
             <br>
           <br />
        </section>
                </body>
 
oke so a little update I'm almost done but I still need it to change only the one that the user changes and not everything.
example of what happens now:
1. 1642516962228.png
2. change quantity for hamburger 1642516997351.png
3. it changes for everything????
1642517025243.png
example of what I want to happen:
I want that it just changes for the one the user decides to change can someone help me

PHP:
<?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;">&#128717;</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){
                        
                         $sql = "SELECT broodjes_ID, broodnaam, prijs, voorraad 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 '<input type="hidden" value="' . $row['broodjes_ID'] . '">';
                                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="broodjes_ID" id="broodjes_ID" value="' . $row['broodjes_ID'] . '">';
                                     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>';
                                 if (!empty($_POST['quantity'])){
                                    $quantity = $_POST['quantity'];
                                    // if ($_POST['broodnaam'] == $_POST['quantity']){
                                         $rowtotaal = $row['prijs'] * $quantity;
                                         $sumtotal += $rowtotaal;
                                 }else{
                                     $_POST['quantity'] =  1;
                                     $quantity = $_POST['quantity'];
                                    // if ($_POST['broodnaam'] == $_POST['quantity']){
                                         $rowtotaal = $row['prijs'] * $quantity;
                                         $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"> leeg winkelmandje</a></button>
                  
            
             <br>
           <br />
        </section>
    </body>
 
Last edited:
PHP:
  if(isset($_POST['quantity'][0])){
      $quantity = $_POST['quantity'];
      $sum += $row['prijs'] * $quantity;
 }else{
     $sum += $row['prijs'];
}
I don't really know much PHP, but it looks like in this part $row['prijs'] * $quantity; you're multiplying the whole price by the quantity* which is why you got 14 and not 11

That's just my guess, I don't really know much PHP.


*if $row['prijs'] is the price and $quantity is the quantity
 
well it'' not solved completely yet. right now I have the problem that it only want to calculate the price * quantity of one item. someone told me I need to store a quantity against each cart item. but I don't understand how to do that this is what I have now
for some reason I can't put any code in here
 

Attachments

  • 1643106148993.png
    1643106148993.png
    45.2 KB · Views: 3
Hello,

do you want to calculate price and total when user changes quantity without form posting or when form is posted?

More friendly solution would be to include JavaScript and listen onchange event on quantity fields.
So when you change value in any quantity field, cart items and cart total is calculated.

Now, you could calculate this in JavaScript or call method in PHP to calculate you items and total and return JSON to display it.
 
Hello,

do you want to calculate price and total when user changes quantity without form posting or when form is posted?

More friendly solution would be to include JavaScript and listen onchange event on quantity fields.
So when you change value in any quantity field, cart items and cart total is calculated.

Now, you could calculate this in JavaScript or call method in PHP to calculate you items and total and return JSON to display it.
I have been trying to use javascript for a while but I'm not good at it. I want to calculate price without the form being posted
 

Latest posts

Buy us a coffee!

Back
Top Bottom