• 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.
    • 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 calculating price based on option


Well-Known Coder
how do I calculate the price based on the option that the user chooses??
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="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>

    body {
        font-size: 20px;

<a class="back" href="index.php"> <i class="bi bi-arrow-left-circle-fill bi-5x"></i></a>
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>
           //dit werkt eerst maar nu werkt het niet meer

            //  print_r($_SESSION['basket']);
            //  var_dump($_SESSION['basket']);
             $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);
                        $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 '<span class="cart-item-title">'. $row['broodnaam'] . '</span>';
                            echo '</div>';
                            echo '<span class="cart-price cart-column"> €'. $row['prijs'] . '</span>';
                            $sum = $row['prijs'];
                            echo '<form name="quantity" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>
                                    <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>
                            echo '</div>';
                            echo '</div>';
                            //$sumtotal = $sum * 2;
                            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 class="cart-total">
                        <strong class="cart-total-title">Total</strong>
                        <span class="cart-total-price"> €
                            function berekenen(){
                                    if ("quantity" == 1) {
                                        $Qty = 1;
                                        $sumtotal = $Qty * $row['prijs'];
                                        echo $sumtotal;
                                    } elseif ("quantity" == 2) {
                                        $Qty = 2;
                                        $sumtotal = $Qty * $row['prijs'];
                                        echo $sumtotal;
                                    } elseif ("quantity" == 3) {
                                        $Qty = 3;
                                        $sumtotal = $Qty * $row['prijs'];
                                        echo $sumtotal;
                                    }elseif ("quantity" == 4){
                                        $Qty = 4;
                                        $sumtotal = $Qty * $row['prijs'];
                                        echo $sumtotal;
                         echo berekenen();
                <div class="cart-total">
                    <strong class="cart-total-title">Total</strong>
                    <span class="cart-total-price"> € 0</span>