Jump to content

Session Data With Further Level Of Information?


son
 Share

Recommended Posts

I have created a basic shopping cart and have now a further task to pass additional info about product (color) to shopping basket. The color can be selected on product page via drop-down and should then display with item description on cart page. My code to add a product to $_SESSION['cart'] is:

if (isset($_GET['id']))	{	$id = (int) $_GET['id'];		if ($id > 0)		{		$id_query = "SELECT product, price FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			if (mysqli_num_rows($id_result) == 1)			{				list ($product, $price) = mysqli_fetch_array($id_result, MYSQLI_NUM);				if (isset($_SESSION['cart'][$id]))				{				$_SESSION['cart'][$id] += $qty2;				echo "<p>Another copy of the following item has been added to your cart: <strong>$product</strong>.</p>\n";				}				else				{				$_SESSION['cart'][$id] = $qty2;				echo "<p>The following item has been added to your cart: <strong>$product</strong>.</p>\n";				}			}		}	}

I read the intro about sessions, but still do not get it how you would add another layer of information (the colour id) to each $_SESSION['cart'][$id]? The way I tried this first ($_SESSION['cart'][$colour] = $colour;) would display for each product the same id. Then I thought maybe $_SESSION['cart'][$id] =$colour would work, but for obvious reasons that overrides the quantity info which I need. My last attempts was $_SESSION['cart'][$id][$colour] =$colour, but this also did not the trick.How do you do that sort of thing?Son

Link to comment
Share on other sites

You know, it never even occurred to me to store session basket info in the session - I use a database table. I'm wondering which is better...You would need to go further into the embedded arrays and add a layer like:$_SESSION['cart'][$id]['instance']['colour'] where 'instance' is which of x products with id = $id is associated with which colour. However, I have thus far avoided using such complex arrays and haven't really got a scooby how to make that work in practical terms...

Link to comment
Share on other sites

You can, but how do you marry the two together? And how do you allow more than one colour per product id?

Link to comment
Share on other sites

Use $_SESSION['cart'][$id]['qty'] for the quantity, and $_SESSION['cart'][$id]['colour'] for the color.
This does help me with the different treatment for colour (for internal reasons I call variable $fn) and quantity, but threw up another issue. Maybe someone wants to add the product in black and in white. The code for adding now is:
if ($id > 0)		{		$id_query = "SELECT product, price FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			if (mysqli_num_rows($id_result) == 1)			{				if ($fn)				{				$_SESSION['cart'][$id]['fn'] = $fn;				}			list ($product, $price) = mysqli_fetch_array($id_result, MYSQLI_NUM);				if (isset($_SESSION['cart'][$id]))				{				$_SESSION['cart'][$id]['qty'] += $qty2;				echo "<p>Another copy of the following item has been added to your cart: <strong>$product</strong>.</p>\n";				}				else				{				$_SESSION['cart'][$id]['qty'] = $qty2;				echo "<p>The following item has been added to your cart: <strong>$product</strong>.</p>\n";				}			}		}

How would I best treat this possibility? Also, I am very interested about benefits of storing cart data in sessions or in database. This website I am working on has also a registration system and registered user shoudl be able to save their cart data for late retrieval and updating. Any suggestions appreciated...Son

Link to comment
Share on other sites

Regarding the colour thing, I believe what he means is that if you assign a colour to a product id, then every item with the same product id will be the same colour, even if your product id covers all possible colours of that product. Hence my original suggestion of a sub-array holding the product instance - so for example, product id 1, instance 1, colour red; product id 1, instance 2, colour black; etc.

Link to comment
Share on other sites

Regarding the colour thing, I believe what he means is that if you assign a colour to a product id, then every item with the same product id will be the same colour, even if your product id covers all possible colours of that product. Hence my original suggestion of a sub-array holding the product instance - so for example, product id 1, instance 1, colour red; product id 1, instance 2, colour black; etc.
You are right, this is what I meant. Someone might add the same t-shirt (same size, brand etc) in black and then adds another one in different colour. Not sure what would be best. Saw your example, but getting lost now myself. With the saving: Sessions are fine for user who are not registered, but if someone is registered and wants to save data I would like to have the session data being transfered into database and if that person logs in automatically populate the cart session data with all stored in database (to be able to manipulate it in same way as other users). Does this sounds plausible? OR do I just walk around the block with this?Son
Link to comment
Share on other sites

For saving the cart information, don't use the product ID as a key, just have an array of products where for each one you can store the product ID, quantity, options, etc.For the database issue, to keep it working with normal users you'll need to write all of the cart information to the database every time you update it, and when a page loads first check if they're logged in and if so populate the session with whatever's in the database. You can use the serialize and unserialize functions to save an array in the database and get it out again, so you can serialize $_SESSION['cart'] and save that in the database.

Link to comment
Share on other sites

For saving the cart information, don't use the product ID as a key, just have an array of products where for each one you can store the product ID, quantity, options, etc.For the database issue, to keep it working with normal users you'll need to write all of the cart information to the database every time you update it, and when a page loads first check if they're logged in and if so populate the session with whatever's in the database. You can use the serialize and unserialize functions to save an array in the database and get it out again, so you can serialize $_SESSION['cart'] and save that in the database.
I will have a go...Son
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...