Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello, I'm building a shopping cart. in this code it should be that whenever you click on the add to cart button it knows the exact broodjes_ID, broodnaam and prijs. from that item. Then it need to show that into the shopping cart at the bottom. but right now its only showing the last shopping item in that box. what am I doing wrong?

PHP
<pre><!-- session_start();  geeft error a sessin has already started -->
<?php 
error_reporting(E_ALL);
ini_set('display_errrors', '1');
?>
<!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>Shop Homepage - Start Bootstrap Template</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-family: Arial, Helvetica, sans-serif;
        font-size: 20px;
        }

        #myBtn {
        display: none;
        position: fixed;
        bottom: 20px;
        right: 30px;
        z-index: 99;
        font-size: 18px;
        border: none;
        outline: none;
        background-color: black;
        color: white;
        cursor: pointer;
        padding: 25px;
        border-radius: 50px;
        }

        #myBtn:hover {
        background-color: #555;
        }
    </style>
    <body>
        <!-- Navigation-->
        <?php include "navbar.php"?>
        <?php if(isset($_SESSION['USER_ID'])){ ?>
        <!-- Header-->
        <header class="bg-dark py-5">
            <div class="container px-4 px-lg-5 my-5">
                <div class="text-center text-white"> <!--Het werkt -->
                    <h1 class="display-4 fw-bolder">Goedemorgen, <?php echo $_SESSION['USERNAME'];?></h1>
                    <p class="lead fw-normal text-white-50 mb-0">Shop in style</p>
                </div>
            </div>
        </header>
        <?php
        } else {
        ?>
                <!-- Header-->
                <header class="bg-dark py-5">
            <div class="container px-4 px-lg-5 my-5">
                <div class="text-center text-white"> <!--het werkt-->
                    <h1 class="display-4 fw-bolder">Goedemorgen, Gast</h1>
                    <p class="lead fw-normal text-white-50 mb-0">Shop in style</p>
                </div>
            </div>
        </header>
        <?php } ?>
        <!--miscchien dit toch in een whileloop gooien dat zal ook de code korter maken-->
        <!-- Section-->
        <button onclick="topFunction()" id="myBtn" title="Go to top"></button> 
        <?php 
        include "config.php";
        $sql  = 'SELECT * FROM broodjes';
        $stmt = $conn->prepare($sql); 
        $stmt->execute();
        $result = $stmt->get_result(); // get the mysqli result
        while($row = $result-> fetch_assoc()){
            $data = array();
            //echo $row['broodnaam'];
            echo '        <section class="py-5">
            <div class="container px-4 px-lg-5 mt-5">
                <div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
                    <div class="col mb-5">
                        <div class="card h-100">
                            <!-- Product image dit kan later nog wel-->
                            <div class="fw-image">
                                <img class="card-img-top" src="https://dummyimage.com/450x300/dee2e6/6c757d.jpg" alt="..." />
                            </div>
                            <!-- Product details-->
                            <div class="card-body p-4">
                                <div class="text-center">
                                    <div class="testing">
                                        <h5 class="broodjes_id" hidden>'.$row['broodjes_ID']. '</h5>
                                    </div>
                                    <!-- Product name-->
                                    <div class="testing">
                                        <h5 class="fw-bolder">'.$row['broodnaam']. '</h5>
                                    </div>
                                    <!-- Product price-->
                                    <div class="test2">
                                        <h3> Prijs:  </h3>   <h3 class="fw-price"> €'.$row['prijs']. '</h3><br>
                                    </div>
                                    <!--voorraad--> 
                                    '; if($row['voorraad'] == 0){
                                        echo '
                                        <h3 class="fw-voorraad" style="color: red;"> uitverkocht</h3><br>
                                    '; }else{ echo ' 
                                        Voorraad: '.$row['voorraad'].'<br>
                                        </div>
                                        </div>
                                        <!-- Product actions-->
                                        <form action="" method="post">
                                        <div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
                                            <div class="text-center"><button class="btn btn-outline-dark mt-auto shop-item-button" type="submit" name="add_to_cart" method="post">Add to cart</button></div>
                                        </div>
                                        </form>
                                    '; } 
                        echo '</div>
                    </div>
                </div>
            </div>
        </section>';
        
        $_SESSION['basket'] = array(); // dit is nu hard coded en moet je ervoor zorgen dat hij dit kan toevoegen als het op de knop word gedrukt
        // when button clicked array_push to $_SESSION['basket'] not working
            if(isset($_POST['add_to_cart'])){
                array_push($_SESSION['basket'], $row['broodjes_ID'], $row['broodnaam'], $row['prijs']);
            } 
        }

        print_r($_SESSION['basket']);
        ?>
        <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 
               // query maken waarbij wij naam en prijs ophalen van de ids.
              
                $count = count($_SESSION['basket']);
                if($count > 0){
                    $sum = 0;
                    for($i = 0; $i<$count; $i++){
                        $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("s", $basket); 
                        $stmt->execute();
                        $result = $stmt->get_result(); // get the mysqli result
                        while($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 ' €'. $row['prijs'] . '';
                            echo '</div>';
                            echo '</div>';
                            
                            $sum += $row['prijs']; 
                        }
                        
                    }
                ?>
                    <div class="cart-total">
                        <?php echo $sum;?>
                    </div>
            <?php

                }else{
                    echo 'mandje is leeg.';

            ?>
                <div class="cart-total">
                    ^__strong class="cart-total-title">Total
                     € 0
                </div>
            <?php 
            }
            ?>



            <div class="text-center">
                <button class="btn btn-primary btn-purchase" style="margin: 5%;" type="button">PURCHASE</button>
            </div>
        </section>

        
        <!-- Footer-->
        <?php include "footer.php";?>
        <!-- Bootstrap core JS-->
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
        <!-- Core theme JS-->
        <script src="js/scrolltotop.js"></script>
    </body>
</html>



What I have tried:

I have tried looking things up on the internet and I understand that you have to push the variables into the arrays and then show it but it's only doing the last one right now and now I'm confused.
Posted
Updated 14-Dec-21 20:51pm
v2
Comments
Rebecca2002 21-Dec-21 3:50am    
can someone help me?
Rebecca2002 21-Dec-21 4:05am    
nevermind I see what I did wrong

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