Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I want to change the price when the user changes the quantity of the product. so if the price = 3 euro and you want 3 items then the code should do 3*3 how do I do this ??

<?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-size: 20px;
    }

    </style>
<body>
<a class="back" href="index.php"> class="bi bi-arrow-left-circle-fill bi-5x"></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_ID = $_GET['broodjes_ID'];
                        
                        // //$basket =  $_SESSION['basket'][$i];
                        $sql = "SELECT broodjes_ID, broodnaam, prijs FROM broodjes WHERE broodjes_ID = ?";
                        // uitvoeren, resultaat tonen in tabel.
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("i", $broodjes_ID); 
                        $stmt->execute();
                        $result = $stmt->get_result(); // get the mysqli result
                        //while loop zorgt er volgens mij voor dat hij blijft optellen
                        if($row = $result-> fetch_assoc()){
                            $sum = 0;     
                            echo '<div class="cart-items">';
                            echo '<div class="cart-row">';
                            echo '<div class="cart-item cart-column">';
                            echo ''. $row['broodnaam'] . '';
                            echo '</div>';
                            echo ' €'. $row['prijs'] . '';
                            $sum = $row['prijs']; 
                            
                            echo '<form name="quantity" method="POST" action="action_page.php">
                                   <select id="quantity" value="quantity">
                                         <option value="1">1</option>
                                         <option value="2">2</option>
                                         <option value="3">3</option>
                                         <option value="4">4</option>
                                     </select>
                                     </form>
                                 '; 
                            echo '</div>';
                            echo '</div>';
                            //$sumtotal = $sum * 2;
                            echo '<br>';
                            echo '<div class="text-center">
                            <button class="btn btn-outline-primary"  type="button"><a href="bestellen.php?broodjes_ID='. $row['broodjes_ID'].'" method="POST">PURCHASE</a></button>
                            </div>';
                            
                        }
                
                        
                    
                ?>
               
                    <div class="cart-total">
                        class="cart-total-title">Total
                         € <?php $sumtotal = $_GET['sumtotal'];  echo $sumtotal;?>
                    </div>
            
                <!-- 
                <div class="cart-total">
                    class="cart-total-title">Total0
                </div>
             -->




        </section>
                </body>

action page
problem with this page tough is that it won't remember the $row['prijs']; variable if you do a separete page

<?php
 $sql = "SELECT broodjes_ID, broodnaam, prijs FROM broodjes WHERE broodjes_ID = ?";
 // uitvoeren, resultaat tonen in tabel.
 $stmt = $conn->prepare($sql);
 $stmt->bind_param("i", $broodjes_ID); 
 $stmt->execute();
 $result = $stmt->get_result(); // get the mysqli result
 //while loop zorgt er volgens mij voor dat hij blijft optellen
 if($row = $result-> fetch_assoc()){
    if($_POST['quantity']){
        if ("quantity" == 1) {
            $Qty = 1;
            $sumtotal = $Qty * $row['prijs'];
            echo '<a href="cart.php?prijs='. $sumtotal.'" method="POST">';
            echo $sumtotal;
        } elseif ("quantity" == 2) {
            $Qty = 2;
            $sumtotal = $Qty * $row['prijs'];
            echo '<a href="cart.php?prijs='. $sumtotal.'" method="POST">';
            echo $sumtotal;
        } elseif ("quantity" == 3) {
            $Qty = 3;
            $sumtotal = $Qty * $row['prijs'];
            echo '<a href="cart.php?prijs='. $sumtotal.'" method="POST">';
            echo $sumtotal;
        }elseif ("quantity" == 4){
            $Qty = 4;
            $sumtotal = $Qty * $row['prijs'];
            echo '<a href="cart.php?prijs='. $sumtotal.'" method="POST">';
            echo $sumtotal;
        }
    }
 }                    
?>


What I have tried:

tried to use get and post method but I kinda wanna use it on the same page
Posted
Updated 10-Jan-22 22:15pm
v7

1 solution

Just us the standard math operators (PHP: Operators - Manual[^]). For example:
PHP
$Qty = 2;
$Price = 3.00;
$Total = $Qty * $Price;
 
Share this answer
 
Comments
Rebecca2002 10-Jan-22 13:14pm    
yes thats also what I wanted to use but how do I make $qty change depending on what option the user chooses? for example user chooses from the dropdown menu option 2 then I want $qty also to be 2 how do I do that
Richard MacCutchan 10-Jan-22 15:06pm    
Rebecca2002 10-Jan-22 15:28pm    
I'm sorry ( maybe I don't understand your anwser) but I did and it just tells me how to make it. I know how to make a select tag with option values in there. I already made one in the code but now I want to do something when the user presses an option( for example when user presses 2 do the calculation that you made) that's what I'm not understanding how to do
Richard MacCutchan 10-Jan-22 16:12pm    
The link I gave you has an example that explains it.
Rebecca2002 10-Jan-22 16:20pm    
it goes to a page called action_page.php??

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