3

I want to use $_SESSION to store items in cart. The items are defined by id, each item has 3 sizes and for each size there will be stored item's quantity. I would like to use multidimensional associative array like that

$_SESSION['cart']['id'.$_GET['id']]['size'.$_POST['size']]['quantity'] += $_POST['quantity'];

but I guess the problem which I am getting (Notice: Undefined index) is because the arrays are not defined first.

I would like to keep it simple, so what would be the easiest way?

koubin
  • 579
  • 5
  • 9
  • 30
  • Are you passing an `id` in your URL, and always sending a `size` and `quantity` in the POST data? Otherwise, there's your problem. – Adam Link May 15 '15 at 02:59
  • yes I do. there is also this in the code: `if(isset($_POST['size'])){...` – koubin May 15 '15 at 03:03

2 Answers2

3

Your issue is that you're just assuming the items are set in $_SESSION. You need to assume they aren't and start by adding them in.

You'd harness isset().

if(!isset($_SESSION['cart']['id'.$_GET['id']])) {
    $_SESSION['cart']['id'.$_GET['id']] = array(
        'sizeONE' => array(
            'quantity' => 0
        ),
        'sizeTWO' => array(
            'quantity' => 0
        ),
        'sizeTHREE' => array(
            'quantity' => 0
        ),
    );
}

You'd obviously modify the above to probably only set the product id as you require then run through the same sort of isset() to add the selected sizes. I'm just showing you how to create the initial structure array.

Darren
  • 13,050
  • 4
  • 41
  • 79
  • I am not assuming they are set. They are not. I just wanted to know the easiest way to do that. Something like the code I wrote above. Maybe something like objects instead of arrays. By the way, how would your definition look like if even `id` is not set? Wouldn't it be too complex? – koubin May 15 '15 at 03:08
  • @koubin Too easy! Well, you'd essentially not store the product (id) in the session if it wasn't set, as you need that to identify the product? – Darren May 15 '15 at 03:09
  • 1
    I don't get the last comment, but I guess, something like that before code would do the trick: `if (!isset($_SESSION['cart'])){$_SESSION['cart'] = array();}` Am I right? – koubin May 15 '15 at 03:15
  • Perfectly right @koubin . What i meant with the last comment is that you wouldn't add a product to the cart session if the user didn't want to add it to there. – Darren May 15 '15 at 03:15
  • 1
    ok. thanks. I hoped for some simple and esthetic solution, but there is probably nothing like I have had in mind. – koubin May 15 '15 at 03:41
1

I'd argue the best manner to store this data isn't in a multidimensional array, but rather in an Object (and not in $_SESSION, but that's a whole different topic).

If you want to approach this with an object, I'd use a variation of the following:

$myItem = new stdClass;
$myItem->id = $_GET['id'];
$myItem->sizeOne = new stdClass;
$myItem->sizeOne->name = "sizeOne";
$myItem->sizeOne->quantity = 1;
// repeat for sizeTwo and sizeThree

$_SESSION['cart'][$myItem->id] = $myItem;

Benefits:

More of an OOP approach to your program.

Drawbacks:

Storing an Object in the $_SESSION may cause scalability issues.

Community
  • 1
  • 1
Adam Link
  • 2,783
  • 4
  • 30
  • 44