Hello, So I managed to do the AddToCart function and stuff but I dont know how to display the cart

this is the add to cart function

<?php
   session_start();

   // get the product id
   $DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
   $name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";

   /* 
    * check if the 'cart' session array was created
    * if it is NOT, create the 'cart' session array
 . */
   if(!isset($_SESSION['cart'])){
     $_SESSION['cart'] = array();
    }

   // check if the item is in the array, if it is, do not add
   if(in_array($DVDID, $_SESSION['cart'])){
      // redirect to product list and tell the user it was added to cart
      header('Location: shop.php?action=exists&id' . $DVDID . '&name=' . $name);
    }

     // else, add the item to the array
    else{
    array_push($_SESSION['cart'], $DVDID);

    // redirect to product list and tell the user it was added to cart
    header('Location: shop.php?action=add&id' . $DVDID . '&name=' . $name);
    }

?>


this is the basket where the items should be displayed

<?php
$action = isset($_GET['action']) ? $_GET['action'] : "";
$Quantity = isset($_GET['Quantity']) ? $_GET['Quantity'] : "";
$DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
$name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";
if($action=='removed'){
    echo "<div>" . $_GET['NameOfTheDVD'] . " was removed from cart.</div>";

}

if (!isset($_SESSION["cart"]) || count($_SESSION["cart"]) < 1) {
 echo 'cart if emepy';
 } else {
if(isset($_SESSION['cart'])){
    $ids = "";
    foreach($_SESSION['cart'] as $DVDID){
        $ids = $ids . $DVDID . ",";
    }

 $ids = rtrim($ids, ',');

 require "connect.php";

 $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD` WHERE DVDID IN ({$ids})";
 $stmt = $dbhandle->prepare( $query );
 $stmt->execute();
 var_dump($_SESSION['cart']);
 $num = $stmt->rowCount();
 $totalQuantity = 0;
 if($num>0){
        echo "<table border='0'>";//start table

            // our table heading
                echo "<tr>";
                echo "<th class='textAlignLeft'>DVD NAME</th>";
                echo "<th>Quantity</th>";
                echo "<th>Action</th>";
                echo "</tr>";
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){


                $totalQuantity += $Quantity;

                //creating new table row per record
                echo "<tr>";
                    echo "<td>" . $row['DVDID'] . "</td>";
                    echo "<td>" . $row['NameOfTheDVD'] . "</td>";
                    echo "<td class='textAlignCenter'>";
                        echo "<a href='RemoveFromCart.php?id={$DVDID}&name={$name}' class='customButton'>";//not working
                            echo "<img src='remove-from-cart.png' title='Remove from cart' />";
                        echo "</a>";
                    echo "</td>";
                echo "</tr>";
            }

            echo "<tr>";
                echo "<th class='textAlignCenter'>Total Quantity</th>";
                echo "<th class='textAlignRight'>{$totalQuantity}</th>";
                echo "<th></th>";
            echo "</tr>";

        echo "</table>";
        echo "<br/><div><a href='#' class='customButton'>Checkout</a></div>";
    }else{
        echo "<div>No products found in your cart.</div>";
    }

}else{
    echo "<div>No products in cart yet.</div>";
}
?>

Any help would be appriciated thanks

Are you getting an erorr?

need some more info

Well, no I am not getting any error. It is just giving me this message "No producst found in this cart"

start by trying to figure out which reason your getting that message

which test is it failing

btw I am getting a different kind of error message now

Invalid argument supplied for foreach() in

I used a debug code var_dump($_SESSION['cart']);

This is the result I got string(0)

are these separate pages do you have a session_start on the display page

They are on the same page and yes I dp

your errors indicate that your $_SESSION['cart'] is empty and not an array
are you sure theres something in it

Thats what I am thinking as welll. I dont know how to pass the data into the Session ['cart']

This is the code in my AddToCart can you check if I wrote the code correctly?

<?php
   session_start();

   // get the product id
   $DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
   $name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";

   /* 
    * check if the 'cart' session array was created
    * if it is NOT, create the 'cart' session array
 . */
   if(!isset($_SESSION['cart'])){
     $_SESSION['cart'] = $_GET['NameOfTheDVD'];
     $_SESSION['cart'] = $_GET['DVDID'];
     $_SESSION['cart'] = $_GET['Quantity'];
     $_SESSION['cart']=$name;

    }

   // check if the item is in the array, if it is, do not add
   if(in_array($DVDID, $_SESSION['cart'])){
      // redirect to product list and tell the user it was added to cart
      header('Location: shop.php?action=exists&DVDID' . $DVDID . '&name=' . $name);
    }

     // else, add the item to the array
    else{
    array_push($_SESSION['cart'], $DVDID, $name);
    // redirect to product list and tell the user it was added to cart
    header('Location: shop.php?action=add&DVDID' . $DVDID . '&name=' . $name);
    }

?>

your current $_SESSION['cart'] = $_GET['NameOfTheDVD']; is setting cart to a string equal to $_GET['NameOfTheDVD'] try something more like this

 if(!isset($_SESSION['cart'])){
    $_SESSION['cart'][$_GET['DVDID']][name] = $_GET['NameOfTheDVD'];
    $_SESSION['cart'][$_GET['DVDID']][qty] = $_GET['Quantity'];
}

once you do add it to the session you must reload before you can do

isset($_SESSION['cart'][$_GET['DVDID']])

array(3) { [""]=> array(2) { ["name"]=> NULL ["qty"]=> NULL } [0]=> string(0) "" [1]=> string(0) "" }

This is what I am getting now, but it is still not displaying the products in the basket page

Thanks for the help by the way.

Can you explain what this code does please, I am kind of unsure

if(isset($_SESSION['cart'])){
        $ids = "";

        foreach($_SESSION['cart'] as $name){
           $ids = $ids . $DVDID . ",";

        }

        // remove the last comma
        $ids = rtrim($ids, ',');

        require "connect.php";

        $query = "SELECT DVDID, NameOfTheDVD, Quantity FROM DVD WHERE DVDID IN ({$ids}) ";
        $stmt = $dbhandle->prepare( $query );
        $stmt->execute();

theWhere DVDID IN ({$ids})

I am kind of confused especially about the ids bit, because when I remove it seems to display the table with just the quantity in but when I put it back, it just comes back with "No producst found in the cart". It seems as thought when the this this bit is present Where DVDID IN ({$ids}) it just skips exeucting the query and displays "No producst found"

looks like your $_GET vars are all empty

how are you passing your variables

Through the session, are they meant to be passed like that?

$action = isset($_GET['action']) ? $_GET['action'] : "";
$Quantity = isset($_GET['Quantity']) ? $_GET['Quantity'] : "";
$DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
$name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";

where do these values come from

From the database, thats where I think I am going wrong isn't that bit meant to come from the shop.php?

i would think so...

are you submiting them from a form? using what method?

I just did a vardump on the shop page and it turns out there is nothing there and I did another in AddToCart and it turns out there is nothing there either. I must be doing SOMETHING really wrong in AddToCart and shop

whats your method of getting them from the shop page to the cart page

Just sessions and a button for the add to cart bit

whats the code for the button

`echo "<a href='AddToCart.php?DVDID={$DVDID}&name={$name}' class='customButton'>";

` session_start();
   // get the product id
   $DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
   $name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";
   require "connect.php";
   $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`";
   $stmt = $dbhandle->prepare($query);
   $stmt->execute();

   /* 
    * check if the 'cart' session array was created
    * if it is NOT, create the 'cart' session array
 . */
   if(!isset($_SESSION['cart'])){
     $_SESSION['cart'][$DVDID];
     $_SESSION['cart'][$name];
     $_SESSION['cart'][$Quantity];

    }

   // check if the item is in the array, if it is, do not add
   if(in_array($DVDID, $_SESSION['cart'])){
      // redirect to product list and tell the user it was added to cart
      header('Location: shop.php?action=exists&DVDID' . $DVDID . '&name=' . $name);
    }

     // else, add the item to the array
    else{
    array_push($_SESSION['cart'][] = $DVDID);
    // redirect to product list and tell the user it was added to cart
    header('Location: shop.php?action=add&DVDID' . $DVDID . '&name=' . $name);
    }
    var_dump($_SESSION['cart']);`

dump your $_GET and post it here

array(0) { } This is what I got

array(3) { ["action"]=> string(6) "exists" ["DVDID"]=> string(0) "" ["name"]=> string(0) "" } 

This is what I got? I forgot to add the product lol

so your not sending $_GET["quantity"] at all
and you looking for $_GET["nameofdvd"] but passing it $_GET["name"]

and for somereason your $_GET["dvdid"] is just empty
check your output are you sure its in the link at all?

yh the $DVDID is in the link header('Location: shop.php?action=add&DVDID' . $DVDID . '&name=' . $name);

is it this? I am not sure if I wrote this right

Edit I changed &name to &NameOfTheDVD

I am also not sure how to send the variables across. I am a bit confused

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.