Session for Shopping basket deduct quantity

Hey,

I am trying to figure something out in regards to a shopping basket. I store the items of the shopping basket in a session. Now i have a ‘remove’ link which should remove the items in a session. See this image:

http://www.glofamily.com/glo/images/step-1.jpg

So you can see that the Wingman T-Shirt has been added twice so x2. When i click the remove link i only want to remove one of the items in the session to leave it as x1 and so on, however it currently removes the whole item.

This is my code:


<?php
error_reporting(E_ALL);
require_once('Models/Cart.class.php');

$cart = new Cart('localhost', 'root', '', 'test');
session_start();

      if(!empty($_SESSION['ID'])) {
        foreach($_SESSION['ID'] as $i => $id) {
          if($_GET['ID'] == $id && $_SESSION['quantity'][$i] > 1) {
            $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] - 1;
          } else {
          $result = $cart->removeItem();
          }
        }
      }

header('Location: homepage');
?>

And this is the method i use to remove the session item:


    public function removeItem(){
        $id = $_GET['ID'];
        unset($_SESSION['ID'][$id]);
        unset($_SESSION['theName'][$id]);
        unset($_SESSION['price'][$id]);
        unset($_SESSION['image'][$id]);
        unset($_SESSION['quantity'][$id]);
    }

What am i doing wrong?

Thanks

Can anyone see a problem with the above code?

Am i doing something wrong?

Thanks

Ahh, of course!

if(!empty($_SESSION['ID'])) {
    foreach($_SESSION['ID'] as $i => $id) {
        if($_GET['ID'] == $id){
            if($_SESSION['quantity'][$i] > 1) {
                $result = $cart->removeOneItem($_GET['ID']);
            } else {
                $result = $cart->removeItemFully($_GET['ID']);
            }
        }
    }
} 

Hey,

Thanks again but it still doesn’t work. This is what i have:


<?php
error_reporting(E_ALL);
require_once('Models/Cart.class.php');

$cart = new Cart('localhost', 'root', '', 'test');
session_start();

if(!empty($_SESSION['ID'])) {
    foreach($_SESSION['ID'] as $i => $id) {
        if($_GET['ID'] == $id){
            if($_SESSION['quantity'][$i] > 1) {
                $result = $cart->removeOneItem($_GET['ID']);
            } else {
                $result = $cart->removeItemFully($_GET['ID']);
            }
        }
    }
}

header('Location: homepage');
?>

I tried just doing this ‘$result = $cart->removeItemFully($_GET[‘ID’]);’ and this function worked, but ‘$result = $cart->removeOneItem($_GET[‘ID’]);’ does not work.

When i try the above code and i have one item with 2 quantities in my basket, nothing happens…

Any ideas? :shifty:

Sorry about this…

ok well it’s not working properly :frowning:

I have tried with this and it deletes ALL quantities of an item, instead of just one.

This is my full cart class:


<?php
class Cart {

    public function sanitise($data) { // Escapes parameters before sending SQL query
        foreach($data as $key => $value){
            $data[$key] = $this->mysqli->real_escape_string($value);
        }
        return $data;
    }

    public function __construct($host, $username, $password, $dbname) {
        $this->mysqli = new mysqli($host, $username, $password, $dbname);
        if ($this->mysqli->errno){
            echo 'Unable to connect'.$this->mysqli->error;
            exit();
        }
    }

    public function AddToCart(){
        !isset($_SESSION['ID']) ? $_SESSION['ID'] = array() : '';
        !isset($_SESSION['theName']) ? $_SESSION['theName'] = array() : '';
        !isset($_SESSION['quantity']) ? $_SESSION['quantity'] = array() : '';
        !isset($_SESSION['price']) ? $_SESSION['price'] = array() : '';
        !isset($_SESSION['image']) ? $_SESSION['image'] = array() : '';

        array_push($_SESSION['ID'], $_POST['ID']);
        array_push($_SESSION['theName'], $_POST['theName']);
        array_push($_SESSION['quantity'], $_POST['quantity']);
        array_push($_SESSION['price'], $_POST['price']);
        array_push($_SESSION['image'], $_POST['image']);
        $loc = $_SERVER['HTTP_REFERER'];
        echo "<script>window.location.href='".$loc."'</script>";
    }
    
    public function removeOneItem($id){
    $item =& $_SESSION['price'][$id];
    $item--;
        if($item == 0){
            removeItemFully($id);
        }
    }

    public function removeItemFully($id){
        unset($_SESSION['ID'][$id]);
        unset($_SESSION['theName'][$id]);
        unset($_SESSION['price'][$id]);
        unset($_SESSION['image'][$id]);
        unset($_SESSION['quantity'][$id]);
    }

    public function removeItem(){
        $id = $_GET['ID'];
        removeOneItem($id);
    }

    public function emptyCart(){
        unset($_SESSION['ID']);
        unset($_SESSION['theName']);
        unset($_SESSION['price']);
        unset($_SESSION['image']);
        unset($_SESSION['quantity']);
    }

    public function __destruct() {
        $this->mysqli->close();
    }
}

And then i do this on the remove item page:


      if(!empty($_SESSION['ID'])) {
        foreach($_SESSION['ID'] as $i => $id) {
          if($_GET['ID'] == $id && $_SESSION['quantity'][$i] > 1) {
            $result = $cart->removeOneItem($_GET['ID']);
          } else {
          $result = $cart->removeItemFully($_GET['ID']);
          }
        }
      }

Can you see a problem anywhere?

Hey,

Thanks for the code. So when i want to check this would i do something like this:


      if(!empty($_SESSION['ID'])) {
        foreach($_SESSION['ID'] as $i => $id) {
          if($_GET['ID'] == $id && $_SESSION['quantity'][$i] > 1) {
            $result = $cart->removeOneItem($id);
          } else {
          $result = $cart->removeItemFully($id);
          }
        }
      }

And also thanks for the advice, i just have this one amend left to do and then i will be changing bits of the shopping cart as many people have advised me to :rolleyes:

Anyway, does the code above look right?

Thanks

Code looks fine to me :slight_smile:

Rather than unsetting the whole array, you want to simply subtract one.


public function removeOneItem($id){
    $item =& $_SESSION['price'][$id];
    $item--;
    if($item == 0){
        removeItemFully($id);
    }
}
public function removeItemFully($id){
    unset($_SESSION['ID'][$id]);
    unset($_SESSION['theName'][$id]);
    unset($_SESSION['price'][$id]);
    unset($_SESSION['image'][$id]);
    unset($_SESSION['quantity'][$id]);
}
public function removeItem(){
    $id = $_GET['ID'];
    removeOneItem($id);
}

Personally, I really wouldn’t have gone about the array like that. It’d make much more sense to have an item in the cart for each product, and that item being an array holding price etc, for example:


$_SESSION['cart'] = array(
    {ProductID} => array(
        'ID' => {ID},
        'Name' => {Name}.
        'Quantity' => {Quantity}
    )
)

That way you can easily iterate through the cart using foreach(), have a good count of the items, maintain integrity and also remove an item with one unset call.

I’d also recommend against storing the image and price in the session. Those should be accessed from the database on request.