Storing multiple values in a $_SESSION variable with PHP
Asked Answered
S

1

8

I'm creating a site which has a shopping cart. I do not need any special functionality so I'm creating the cart on my own rather than integrating any ready one. My products do not have a predefined price in the database. The price is being generated dynamically based on the values entered by a user on the product page. So, the user chooses some specifications, enters the quantity and I get the following values:

Item ID
Quantity
Total price

I need to store those values in the $_SESSION variable and then loop over it when needed to get the results and print them in the shopping cart. The problem is that there are a lot of products and I need to store all those values (Quantity, Total Price) distinctively for the chosen product. That said, how do I store Item ID, Quantity and Total price in the $_SESSION variable and associate those values with each other?

Thanks for helping.

EDIT: My code implementing Michael's suggestions:

$itemid = $db->escape($_POST['productid']);
    $itemquantity = $db->escape($_POST['itemquantity']);
    $totalprice = $db->escape($_POST['totalprice']);

    $_SESSION['items'] = array();

    $_SESSION['items'][$itemid] = array('Quantity' => $itemquantity, 'Total' => $totalprice);

    var_dump($_SESSION);
Stylet answered 22/1, 2012 at 20:37 Comment(2)
simple multidimensional array would do.Fusty
$_SESSION[] itself is an array. you can store arrays into it too.Euphorbia
C
11

Use the item ID as an array key, which holds an array of the other items:

// Initialize the session
session_start();

// Parent array of all items, initialized if not already...
if (!isset($_SESSION['items']) {
  $_SESSION['items'] = array();
}

// Add items based on item ID
$_SESSION['items'][$itemID] = array('Quantity' => $quantity, 'Total' => $total);
// Another item...
$_SESSION['items'][$another_itemID] = array('Quantity' => $another_quantity, 'Total' => $another_total);
// etc...

And access them as:

// For item 12345's quantity
echo $_SESSION['items'][12345]['Quantity'];

// Add 1 to quantity for item 54321
$_SESSION['items'][54321]['Quantity']++;
Cageling answered 22/1, 2012 at 20:39 Comment(12)
Thanks Michael! However this erases the existing values in the $_SESSION variable. Say, I've added one set of values with $itemID=29 and then when I add another one with $itemID=30 my $_SESSION variable contains only the last set of data.Stylet
@Stylet Not so. $_SESSION['items'] is an array. Each time you add an item to it with $_SESSION['items'][$newitemID] you're adding to the array, not overwriting the previous one.Cageling
When I do a simple print_r($_SESSION) it prints only the last set of values, not all.Stylet
@Stylet Post your current code implementing what I have above, and the var_dump($_SESSION)Cageling
I've updated my question with the code which returns only the last result set.Stylet
@Stylet I see - it's because you're calling $_SESSION['items'] = array() each time before adding a new item. You're re-initializing the array each time so only the last item appears there. Just call $_SESSION['items'] = array(); one time at the very beginning before any items have been added, and don't call it again.Cageling
Thanks for helping me, Michael. But it still does not work as expected even if I completely remove $_SESSION['items'] = array(); from the code.Stylet
@Stylet And you have called session_start()? I made a couple of additions above..Cageling
Thanks Michael! My fault, I forgot about session_start(); This works fine now.Stylet
@Stylet And I'd be careful about storing HUGE arrays here. It's generally not great practice to store tons of things in the session variable...Pastis
@MichaelBerkowski I am working with PHP Add-To-Cart module using SESSION, if i click add to cart button only one product will be added to cart, how can i use to add more than one products added to the cart using session? my code : pastiebin.com/5d25daaf24674 thanks.Asbestos
@Asbestos That is too broad to be addressed in an old question's comment thread. You should post a new question, reference this one if you are attempting to apply it to your situation.Cageling

© 2022 - 2024 — McMap. All rights reserved.