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 keep item in cart

220061

Well-Known Coder
hello I'm creating a shopping cart with an array but it doesn't keep the selected item in cart. how do I keep it in the cart?

PHP:
<?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>Cart</title>
        <!-- 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>

    </head>
    <style>
    body {
       
        font-size: 20px;
    }

    </style>
<body>
<a class="back" href="index.php"> &#x2190;</a>
<?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()){
    $_SESSION['basket'] = array();
   
    if(isset($_POST['add_to_cart'])){
            array_push($_SESSION['basket'], $row['broodjes_ID'], $row['broodnaam'], $row['prijs']);
        }
    }
        ?>
        <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>
            </div>
            <?php
            //misschine kan dit helpen https://stackoverflow.com/questions/35164397/php-shopping-cart-array probleem is alleen snap de code niet
               // query maken waarbij wij naam en prijs ophalen van de ids.
               $broodjes_ID = $_GET['broodjes_ID'];
                $count = count($_SESSION['basket']); // count basket puts 3 times the same item in the shopping cart
                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("i", $broodjes_ID);
                        $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 '<span class="cart-item-title">'. $row['broodnaam'] . '</span>';
                            echo '</div>';
                            echo '<span class="cart-price cart-column"> €'. $row['prijs'] . '</span>';
                            echo '</div>';
                            echo '</div>';
                           
                            $sum += $row['prijs'];
                        }
                       
                    }
                ?>
                    <div class="cart-total">
                        <strong class="cart-total-title">Total</strong>
                        <span class="cart-total-price"> € <?php echo $sum;?></span>
                    </div>
            <?php

                }else{
                    echo 'mandje is leeg.';

            ?>
                <div class="cart-total">
                    <strong class="cart-total-title">Total</strong>
                    <span class="cart-total-price"> € 0</span>
                </div>
            <?php
            }
            ?>



            <div class="text-center">
                <button class="btn btn-outline-primary"  type="button"><a href="bestellen.php">PURCHASE</a></button>
            </div>
        </section>
</body
 
Last edited:
example of what happens when you do add to cart
1640077170730.png
1640077189276.png
but the you press the back button and try to add another thing to cart again and the previous item is gone how do I fix this??
1640077248265.png
where is the previous item???
1640077265725.png
 
Yeah but i see no

session_start();

You need to put this before you try and get the session variable, ideally you need this on any page your going to use $_SESSION['basket'] 🙂
 
@simong1993 been trying it for the past few weeks but still no luck items don't stay in cart
PHP:
<?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/[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>
    </head>
    <style>
    body {
        font-size: 20px;
    }
    </style>
<body>
<a class="back" href="index.php"> <i class="bi bi-arrow-left-circle-fill bi-5x"></i></a>
<?php
include "config.php";
 
     $_SESSION['basket'] = array();
     array_push($_SESSION['basket'], $_GET['broodjes_ID']);
    $_SESSION['basket'][] = $_GET['broodjes_ID'];
    // oke het is neit geweldig maar het laat iets s
        ?>
        <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>
            </div>
            <?php
         
               $broodjes_ID = $_GET['broodjes_ID'];
                //onthouden van vorige get request
                //$count = count($_SESSION['basket']); // count basket puts 3 times the same item in the shopping cart
                if($_SESSION['basket'] > 0){
                 
                    for($i = 0; $i<$_SESSION['basket']; $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("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 '<span class="cart-item-title">'. $row['broodnaam'] . '</span>';
                            echo '</div>';
                            echo '<span class="cart-price cart-column"> €'. $row['prijs'] . '</span>';
                            echo '</div>';
                            echo '<span class="cart-price cart-column"> 0</span>';
                            echo '</div>';
                            $sum = $row['prijs'];
                            break;
                         
                         
                         
                        }
                     
                    }
                ?>
                    <div class="cart-total">
                        <strong class="cart-total-title">Total</strong>
                        <span class="cart-total-price"> € <?php echo $sum;?></span>
                    </div>
            <?php
                }else{
                    echo 'mandje is leeg.';
            ?>
                <div class="cart-total">
                    <strong class="cart-total-title">Total</strong>
                    <span class="cart-total-price"> € 0</span>
                </div>
            <?php
            }
            ?>


            <div class="text-center">
                <button class="btn btn-outline-primary"  type="button"><a href="bestellen.php">PURCHASE</a></button>
            </div>
        </section>
</body
 
I might have a idea what the problem is but not how to fix it how do I show the items that are in the array and not show the items based on the id from the link?
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom