Click here to Skip to main content
15,312,353 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;
   
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??
Richard MacCutchan 10-Jan-22 16:35pm
   
Yes, the information is posted back to the PHP code on the page. So your PHP code needs to deal with the returned value (tagged 'quantity' in the posted data) and do the calculations. It can then rebuild the page with the newly calculated value.
Rebecca2002 11-Jan-22 1:14am
   
but my price depends on my $row['price'] value how will I bring this there????
Richard MacCutchan 11-Jan-22 3:41am
   
The selected item of the select list is sent to the back end code when the form is submitted. The backend then needs to process that information and rebuild the page with the correct values. This is standard PHP processing, the same as submitting any information from a web page.
Rebecca2002 11-Jan-22 3:52am
   
wait I will show you what I have right now I will update the post with the extra file because I don't think I can explain it very well. I do understand the part of the backend my problem still is with calculating it in the seperate file. because $row['prijs'] how will I tell the file to use that right there it doesn't seem to understand it right now? maybe make new connection??
Richard MacCutchan 11-Jan-22 3:59am
   
If you are going to post it to a separate page, then you need to pass all the required variables to that page.
Rebecca2002 11-Jan-22 4:16am
   
updated the post I'm so sorry I guess the probelm is that when I select something inside the dropdown it doesn't go to the action page and because of this it doesn't show the price
Rebecca2002 11-Jan-22 1:16am
   
@Richard-MacCutchan right now tried to do it on the same page but no results
Richard MacCutchan 11-Jan-22 7:58am
   
There is a complete tutorial at PHP Select Option[^].

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