Click here to Skip to main content
15,314,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello I'm trying to make my page more dynamic and I want to array_push broodjes_ID into my array $_SESSION['basket']; how do I do this? I'm a little confused because I aldo have html code in there for the styling and I don't wanna mess that up

<!-- 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 
            $_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
        ?>
        <?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-->
                                        <div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
                                        <form action="array.php" method="post">
                                            <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>
                                        </form>
                                        </div>
                                    '; } 
                        echo '</div>
                    </div>
                </div>
            </div>
        </section>';
        // when button clicked array_push to $_SESSION['basket'] not working
            if(isset($_POST['add_to_cart'])){
                array_push($_SESSION['basket'], $row['broodjes_ID']);
            } 
        }

        ?>


What I have tried:

tried asking teachers. they don't know
tried google but when I ask something about shopping carts I just get a bunch of javascript and I don't wanna use that.
Posted
Updated 14-Dec-21 4:12am
v3

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