Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. 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.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP how to store the quantities alongside the products which the user has added to their cart.

220061

Well-Known Coder
Hello I'm trying to create this shopping cart and I dont understand how to store the quantities alongside the products which the user has added to their cart. I know I need a session but thats all i know. I have tried to do:

PHP:
$_SESSION['quantity'] = $_POST['quantity'];

or making an array and put these two in there

PHP:
$_SESSION['qty'][] = $row['broodnaam'];
$_SESSION['qty'][] =  $_POST['quantity'];

but these things don't work either does anyone else have an idea?
PHP:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errrors', '1');

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
            $broodjes = $_GET['broodjes_ID'];
         
            if (isset($_SESSION['basket'])){
                if( in_array( $broodjes ,$_SESSION['basket']) )
                {
                 
                }else{
                    $_SESSION['basket'][] = $broodjes;
                 
                }
            }else{
                $_SESSION['basket'][]= $broodjes;
             
            }

         
            $sumtotal = 0;
         
                     foreach($_SESSION['basket'] as $key => $value){
                        //session for quantity
                        $_SESSION["qty"] = array();
                         //echo "Key = $key; value = $value; <br>";
                         $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 '<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 method="POST" action"">';
                                     echo '<div class="col-xs-4">';
                                     echo '<input type="hidden" name="broodnaam" id="broodnaam" value="' . $row['broodnaam'] . '">';
                                     echo '<input type="number" name="quantity" id="quantity" class="form-control input-sm" placeholder="'. $_POST['quantity'] .'" min="1" max="100">';
                                     echo '</div>';
                                  echo '</form>';
                                 echo '</div>';
                             
                     
                              //session for quantity???'
                           
                              //store the quantity alongside the products wich the user has added to their cart
                            //   $_SESSION['qty'][] = $row['broodnaam'];
                            //   $_SESSION['qty'][] =  $_POST['quantity']; // deze is bij elke rij hetzelfde??
                             // print_r($_SESSION['qty']);
                                $quantity = 1;
                             
                                    if (isset($_POST['quantity']) && !empty($_POST['quantity'])){
                                        $_SESSION['quantity'] = $_POST['quantity'];
                                        //$qty = $_SESSION["qty"] + 1;
                                        if (isset($_POST['broodnaam']) && !empty($_POST['broodnaam'])){
                                             if ($_POST['broodnaam'] == $row['broodnaam']){
                                                 $quantity = $_POST['quantity'];
                                             }
                                        }
                                    }

                                    echo '<div class="cart-item cart-column">';
                                    $rowtotaal = $row['prijs'] * $quantity;
                                    $sumtotal += $rowtotaal;
                                    echo $rowtotaal;
                                    echo '</div>';

                            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>
               
         
             <br>
           <br />
        </section>
    </body>
 
Last edited by a moderator:
Back
Top Bottom