0

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
2
Contributors
34
Replies
136
Views
3 Years
Discussion Span
Last Post by jstfsklh211
0

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

Edited by Mohamed_26

0

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)

0

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

0

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

0

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);
    }

?>
0

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']])
0

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

0

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"

Edited by Mohamed_26

1
$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

0

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

0

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

0

`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']);`
0
array(3) { ["action"]=> string(6) "exists" ["DVDID"]=> string(0) "" ["name"]=> string(0) "" } 

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

0

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?

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.