Create Shopping Cart Session

Hi all, having a bit of a problem. I’m building an online shop, I’ve now got to the stage where I need to build the shopping cart system. I’m adding the products to the basket using session but I seem to be having a problem, they’re being added but not quite as I like! Please see my code below:

$_SESSION['cart'][]['prod'] = $_POST['prodName'];
$_SESSION['cart'][]['id'] = $_POST['prodId'];
foreach($_SESSION['cart'] as $id=>$value):
    echo "You have added ".$value['prod']." which is product ref!".$value['id']."<br/>";
endforeach;

This is the output when I print the array $_SESSION, I have various products added at the moment:

Array (
[cart] =>
Array ( [0] => Array ( [prod] => Manfrotto 055CX3 Tripod 3 Sections ) [1] =>
Array ( [id] => 1415 ) [2] => Array ( [prod] => Manfrotto 055CX3 Tripod 3 Sections ) [3] =>
Array ( [id] => 1415 ) [4] => Array ( [prod] => Manfrotto 055CX3 Tripod 3 Sections ) [5] =>
Array ( [id] => 1415 ) [6] => Array ( [prod] => Manfrotto 190XDB + MN 804RC2 Head ) [7] =>
Array ( [id] => 1428 ) [8] => Array ( [prod] => Manfrotto 190XDB + MN 804RC2 Head ) [9] =>
Array ( [id] => 1428 ) ) )  

What would you like?

Hi there, I want the id and name to be in the same array but under one large array (cart).

How about this?

<?php

$cart = array();

$arrProductItem = array(“1”=>“Some product”);
array_push($cart, $arrProductItem);

$arrProductItem2 = array(“2”=>“Some product 2”);
array_push($cart, $arrProductItem2);

print_r($cart);

?>

oh…
output is:

Array ( [0] => Array ( [1] => Some product ) [1] => Array ( [2] => Some product 2 ) )

Hey dude, thanks for the message. Managed to fathom it out in the end. Instead of:

        $_SESSION['cart'][]['prod'] = $_POST['prodName'];
        $_SESSION['cart'][]['id'] = $_POST['prodId'];

I replaced it with the following and it’s working perfectly :slight_smile:

        $_SESSION['cart'][$i]['prod'] = $_POST['prodName'];
        $_SESSION['cart'][$i]['id'] = $_POST['prodId'];

Good on ya!

Hey guys, got a small problem, my $_SESSION is only accepting two products, if I had another then it just replaced the second array. Please see my code below:

class cart {
    function addItems(){
        $i=0;
        $_SESSION['cart'][$i]['prod'] = $_POST['prodName'];
        $_SESSION['cart'][$i]['id'] = $_POST['prodId'];
        $i++;
        //print_r($_SESSION);
        //session_destroy();

    }
}

I can see what I’m doing wrong, each time I call the method it starting back at 0 - not sure of a way around this! Really appreciate any help :slight_smile:

Can you show us the form you’re using please? :slight_smile:

Morning buddy, yeah sure here is my form…


<?php
if(isset($_POST['cartadd'])):
$cart = new cart();
$cart->addItems();
endif;
?>

<form action="<?php echo $_SERVER['REQUEST_URI']?>" method="post">
        <input type="hidden" name="prodName" value="<?php echo $result['products_name'] ?>"/>
            <input type="hidden" name="prodId" value="<?php echo $result['products_id'] ?>" />
                <input type="submit" name="cartadd" value="Add to Cart" />
    </form>

Well, ideally you want to store your cart like so…


<?php
$cart = array(
  1 => array(
    'name'      => 'Foo Biscuits',
    'quantity'  => 56,
    'price'     => 1.99
  ),
  2 => array(
    'name'      => 'Bar Sandwich',
    'quantity'  => 3,
    'price'     => 4.99
  ),
);

You would then add a product like this…


<?php
$cart[ $_POST['product_id'] ] = array(
  'name'  => $_POST['product_name']
)

…and iterate over them a bit like…


<?php
foreach($cart as $id => $product){
  printf(
    'You have %d of (%d)%s at %f each' . PHP_EOL,
    $product['quantity'],
    $id,
    $product['name'],
    $product['price']
  );
}

/*
  You have 56 of (1)Foo Biscuits at 1.990000 each
  You have 3 of (2)Bar Sandwich at 4.990000 each
*/

Hey dude, thanks for the message, massive help, also think I’ve worked out a solution, not sure if it’s a good way forward, what are your thoughts?

class cart {
    function addItems(){
        $_SESSION['cart'][$_SESSION['count']]['prod'] = $_POST['prodName'];
        $_SESSION['cart'][$_SESSION['count']]['id'] = $_POST['prodId'];
        $_SESSION['count']++;
        //print_r($_SESSION);
        //session_destroy();

    }
}

I don’t see why ‘count’ is needed?

Hey dude, no I guess it’s not really, ok think I’ll drop that bit, it’s kind of pointless…late night, maybe that’s why I’m being a bit of a noob! Cheers bud :lol:

Ha.

No worries. :slight_smile:

Anthony.

You may look this cart.