Jump to content

Cart Id Issue


son
 Share

Recommended Posts

I have this simple shopping cart application where user can add items with a choice of colour and fabric (there are also variations of the main product, but this is not relevant to my issue). If a user was to select the same item again, but with different colour and/or fabric the previous colour/fabric would be overriden and the quantity increased by one with the new choice. This is not desired, the new combination should be added as a new item as such, but am not sure in how to achieve this as they still have the same product id. My code 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 != 0)				{				$_SESSION['cart'][$id]['fn'] = $fn;				}				if ($var != 0)				{				$_SESSION['cart'][$id]['var'] = $var;				}				if ($age != 0)				{				$_SESSION['cart'][$id]['age'] = $age;				}							list ($product, $price) = mysqli_fetch_array($id_result, MYSQLI_NUM);				if (isset($_SESSION['cart'][$id]))				{				$_SESSION['cart'][$id]['qty'] += $qty2;								}				else				{				$_SESSION['cart'][$id]['qty'] = $qty2;								}			}		}

Any ideas?Son

Link to comment
Share on other sites

You can't use the product ID as the key to store each item, just use a regular numbered array. When they add a product loop through the array to see if you find another entry that matches the ID and options.
And how would I then keep track of what product id is added?Son
Link to comment
Share on other sites

You would store the product ID as another property of the product, you just wouldn't also use it as the array key.
So would then
$_SESSION['cart'][$id]['fn'] = $fn;$_SESSION['cart'][$id]['var'] = $var;$_SESSION['cart'][$id]['age'] = $age;$_SESSION['cart'][$id]['qty'] = $qty2;

change to:

$_SESSION['cart'][$nr][$id]['fn'] = $fn;$_SESSION['cart'][$nr][$id]['var'] = $var;$_SESSION['cart'][$nr][$id]['age'] = $age;$_SESSION['cart'][$nr][$id]['qty'] = $qty2;

? And would I loop like:

if (isset($_SESSION['cart'][$nr])){$_SESSION['cart'][$nr][$id]['qty'] += $qty2;}else{$_SESSION['cart'][$nr][$id]['qty'] = $qty2;}

Son

Link to comment
Share on other sites

$_SESSION['cart'][$nr]['id'] = $id;$_SESSION['cart'][$nr]['fn'] = $fn;$_SESSION['cart'][$nr]['var'] = $var;$_SESSION['cart'][$nr]['age'] = $age;$_SESSION['cart'][$nr]['qty'] = $qty2;
Therefore loop as:
if (isset($_SESSION['cart'][$nr])){$_SESSION['cart'][$nr]['qty'] += $qty2;}else{$_SESSION['cart'][$nr]['id'] = $id;$_SESSION['cart'][$nr]['qty'] = $qty2;if ($fn != 0){				$_SESSION['cart'][$nr]['fn'] = $fn;}if ($var != 0){$_SESSION['cart'][$nr]['var'] = $var;}if ($age != 0){$_SESSION['cart'][$nr]['age'] = $age;}}

? Thanks,Son (I am very grateful for your help, once again)

Link to comment
Share on other sites

That code would assume you've already looped through the cart array to determine what $nr is supposed to be set to, but yeah that's the general idea.
Would I then loop before as
$nr = 0;foreach($_SESSION['cart'] as $key=>$value){$nr = $nr + 1;}

:? Or is there a special way for session data?Son

Link to comment
Share on other sites

In that case $nr will be the number of products, which is the same as count($_SESSION['cart']). You need to stop when you find the product ID you're looking for, you can use a break statement to quit out of the foreach loop. There's an example of that in the other thread about this where I posted the code to build the products array.

Link to comment
Share on other sites

In that case $nr will be the number of products, which is the same as count($_SESSION['cart']). You need to stop when you find the product ID you're looking for, you can use a break statement to quit out of the foreach loop. There's an example of that in the other thread about this where I posted the code to build the products array.
But is the number of existing products in the cart not what I am after? I should use $nr as a sequential number (as you said unrelated to actual product id), so I could potentially have the same item with different colours. The product code which is passed would then be stored as attribute. So, I could have data as (I just write it like this to verify I get this right):$_SESSION['cart'][$nr]['id'] = $id could be $_SESSION['cart'][1][55] or $_SESSION['cart'][2][101] if same item with different colours are passed into cart.Could I not even then go as far as assiging new data as:$nr = count($_SESSION['cart'];$nr + 1;? Or am I now completely off the mark?Son
Link to comment
Share on other sites

I thought you were trying to update a specific product, if you're trying to update a specific product then you need to find where that product is in the cart array and update that entry, so you would need to loop through and find the product first. If you're adding a new product then it would make sense to create the product as a new array and once all of the data is there then just push the new array onto the end of the products array.

Link to comment
Share on other sites

I thought you were trying to update a specific product, if you're trying to update a specific product then you need to find where that product is in the cart array and update that entry, so you would need to loop through and find the product first. If you're adding a new product then it would make sense to create the product as a new array and once all of the data is there then just push the new array onto the end of the products array.
I was on this all day and I still do not get it. I am working now with:
$id_query = "SELECT product_id, product, price FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			if (mysqli_num_rows($id_result) == 1)			{			list ($product_id, $product, $price) = mysqli_fetch_array($id_result, MYSQLI_NUM);				if (isset($_SESSION['cart'][$nr]))				{				$_SESSION['cart'][$nr]['qty'] += $qty2;				echo "<p>Another copy of the following item has been added to your cart: <strong>$product</strong>.</p>\n";				}				else				{			$nr = 0;			foreach($_SESSION['cart'] as $key=>$value){			$nr = $nr + 1;			}				$_SESSION['cart'][$nr]['id'] = $id;				$_SESSION['cart'][$nr]['qty'] = $qty2;				echo "<p>The following item has been added to your cart: <strong>$product</strong>.</p>\n";					if ($fn != 0)					{					$_SESSION['cart'][$nr]['fn'] = $fn;					}					if ($var != 0)					{					$_SESSION['cart'][$nr]['var'] = $var;					}					if ($age != 0)					{					$_SESSION['cart'][$nr]['age'] = $age;					}								}			}

for the first bit of code and this does not work at all. It does not add to the session array and strangely says that it added a new item when this item already exists and and the other way round. I am totally lost. I read the tutorial on session data, but it does not go too much into detail. For example I also do not know how to change the query to display the product data in table from

$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";		foreach($_SESSION['cart'] as $id => $v)		{		$cart_query .= (int) $id . ',';		}	$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY p.product";

I tried (l leave the variations out for now, otherwise I am even more lost)

$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";		foreach($_SESSION['cart'][$nr]['id'] as $id => $v)		{		$cart_query .= (int) $id . ',';		}	$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY p.product";

I am grateful for any help, I am not sure any more if I manage to sort this at all...Son

Link to comment
Share on other sites

$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";foreach($_SESSION['cart'] as $product){  $cart_query .= $product['id'] . ',';}$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY p.product";

For the first piece of code, I still can't tell if you've set $nr correctly. But I can tell you that this:

$nr = 0;foreach($_SESSION['cart'] as $key=>$value){  $nr = $nr + 1;}

does the exact same thing as this:$nr = count($_SESSION['cart']);

Link to comment
Share on other sites

You are right, it does the same thing. In both cases not what I would like. In my desperation I tried to make your example from the other thread work, but the array always only shows the last added item. The code for this is:

if (isset($_GET['id']))	{	$id = (int) $_GET['id'];		if ($id > 0)		{		$id_query = "SELECT product_id, product FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			$products = array();				while ($row = mysqli_fetch_assoc($id_result))				{				$prod_idx = false;  					for ($i = 0; $i < count($products); $i++)  					{    					if ($products[$i]['product_id'] == $row['product_id'])    					{      					$prod_idx = $i;      					break;    					} 						 }  if ($prod_idx === false)  {    // add new product				// add new product    			$prod = array(     			 'product_id' => $row['product_id'],      				'product' => $row['product'],      				'variations' => array()    				);					$prod_idx = count($products);    $products[$prod_idx] = $prod;}// add variation to product  $var = array(    'code' => $row['code'],    'detail' => $row['detail']  );  $products[$prod_idx]['variations'][] = $var;				}				print_r($products);						}	}

I was not entirely sure how this relates to sessions, but I thought I give it a go...Son

Link to comment
Share on other sites

I'm a little confused myself at this point, I'm not sure what problems you're trying to solve now. The code above isn't going to do much, it's just going to add 1 product record to an array called $products. If you use the query that joins the products and variations tables then that loop would make one entry for each product, where each product also has an array for each variation.

Link to comment
Share on other sites

I'm a little confused myself at this point, I'm not sure what problems you're trying to solve now. The code above isn't going to do much, it's just going to add 1 product record to an array called $products. If you use the query that joins the products and variations tables then that loop would make one entry for each product, where each product also has an array for each variation.
I am working with two queries (and I am entirely confused about this myself now). The first one:$id_query = "SELECT product_id, product, price FROM products WHERE product_id = $id";was to see if there is a product stored in database with the id from querystring and if yes, build up a new session if $id does not exist or to add to the relevant existing session if it does exist.The second query was then used to display the data on cart page. I list my old working attempt below:
if ($_SESSION['cart'] && !empty($_SESSION['cart'])){if ($var == 0){$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";}else{$cart_query = "SELECT p.product_id, pv.product, pv.code, pv.detail, pv.price, p.discount_id, p.img1 FROM products as p, productsVar as pv WHERE p.product_id IN (";}foreach($_SESSION['cart'] as $id => $v){$cart_query .= (int) $id . ',';}$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY p.product";

The bit

foreach($_SESSION['cart'] as $id => $v){$cart_query .= (int) $id . ',';

is my biggest issue as $id is not the session key any more, but I really do not know how to run the correct routines to check in database for product that matches $id and session key.Would I need to do both in one query? And if yes, how could I achieve all three objectives: adding a new entry in session array (or updating an existing one obviously which I will address once I get my head around this), displaying data from main products table and/or variations table depending on what variation id has been passed? I am sorry for my ignorance. I was so pleased I got the simple cart version working and find now myself completely confused in how to deal with the fact that the sequence number of array data is not product id any more.Many thanks,Son

Link to comment
Share on other sites

Each entry in the $_SESSION['cart'] array represents a single product ordered with a certain set of options, like quantity and whatever else. So when you loop through it like this:foreach($_SESSION['cart'] as $id => $v)$v is the product. So the product ID will be $v['id'] or $v['product_id'] or whatever the name was. $id is just the array index, that doesn't matter, you don't use that in a query or display it to the user (unless you're printing the items in their cart in a numbered list, and you want to show which number in the list this product is). The quantity should be in $v['qty'], etc.As far as I can tell you really need 2 things. You need once page where they add an item to the cart, and one page to display the cart. You can get into editing and deleting later. When someone adds a new product, and you get the product ID they added and whatever options, you need to loop through the cart array and check to see if the product ID is already there. If it is, then you need to check if all of the options are the same. If it's the same product with the same options then you just update the quantity, otherwise you add a new product and set the ID and options and whatever else. On the page where you display the cart, you loop through the cart array and write out each product name or whatever, and then loop through each product's variations array to write out each option.

Link to comment
Share on other sites

Each entry in the $_SESSION['cart'] array represents a single product ordered with a certain set of options, like quantity and whatever else. So when you loop through it like this:foreach($_SESSION['cart'] as $id => $v)$v is the product. So the product ID will be $v['id'] or $v['product_id'] or whatever the name was. $id is just the array index, that doesn't matter, you don't use that in a query or display it to the user (unless you're printing the items in their cart in a numbered list, and you want to show which number in the list this product is). The quantity should be in $v['qty'], etc.As far as I can tell you really need 2 things. You need once page where they add an item to the cart, and one page to display the cart. You can get into editing and deleting later. When someone adds a new product, and you get the product ID they added and whatever options, you need to loop through the cart array and check to see if the product ID is already there. If it is, then you need to check if all of the options are the same. If it's the same product with the same options then you just update the quantity, otherwise you add a new product and set the ID and options and whatever else. On the page where you display the cart, you loop through the cart array and write out each product name or whatever, and then loop through each product's variations array to write out each option.
Now I get really worried. This sounds complicated. My cart before was nice and easy and now I am really lost. Is there no way that I could just use my existing cart (all code in one page) and then modify in differnt places? If this does not sound like a good idea: do you know by any chance of any good tutorial to do the cart in a way similar to what you said?Son
Link to comment
Share on other sites

You can do it all on one page, you just need one part that adds items, one that displays the items, etc. It will be easier to do if you separate those out and do them one at a time.
Will try then to use what I have and change the bits. One more question, before I try again: My query
if ($_SESSION['cart'] && !empty($_SESSION['cart'])){if ($var == 0){$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";}else{$cart_query = "SELECT p.product_id, pv.product, pv.code, pv.detail, pv.price, p.discount_id, p.img1 FROM products as p, productsVar as pv WHERE p.product_id IN (";}foreach($_SESSION['cart'] as $id => $v){$cart_query .= (int) $id . ',';}$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY p.product";

does not bring me anywhere. How can I retrieve data via product_id when the product_id ($id) is only an attribute of session array (which is numbered with $nr) as $_SESSION['cart'][$nr]['id'] = $id;) to display main products and items? The query is my main issue I think...Son

Link to comment
Share on other sites

foreach($_SESSION['cart'] as $prod){  $cart_query .= $prod['id'] . ',';}

Do you think it is ok without classing it as an integer (just small general question with regard to security)...Also, I run did a dump on query and run it in MySQL to see what is going wrong. The query is:SELECT pv.productVar_id, p.product_id FROM products as p, productsVar as pv WHERE pv.productVar_id = 29 AND pv.parent IN (20,56)and it displays 223 rows of data with a productVar_id of 29 (?) although the productVar_id is the key and only exists once (I even double-checked this). In where clause it clearly states to get productVar_id with a value of 29. I don't get why it would repeat this over and over again...Many thanks,Son Edited by son
Link to comment
Share on other sites

Do you think it is ok without classing it as an integer (just small general question with regard to security)...Also, I run did a dump on query and run it in MySQL to see what is going wrong. The query is:SELECT pv.productVar_id, p.product_id FROM products as p, productsVar as pv WHERE pv.productVar_id = 29 AND pv.parent IN (20,56)and it displays 223 rows of data with a productVar_id of 29 (?) although the productVar_id is the key and only exists once (I even double-checked this). In where clause it clearly states to get productVar_id with a value of 29. I don't get why it would repeat this over and over again...Many thanks,Son
I managed to correct query and it does not repeat the data any more. The query is now:
if ($var == 0){$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";}else{$cart_query = "SELECT p.product_id, pv.product, pv.code, pv.detail, pv.price, p.discount_id, p.img1 FROM products as p, productsVar as pv WHERE pv.productVar_id = $var AND p.product_id IN (";}		foreach($_SESSION['cart'] as $id => $v)		{		$cart_query .= (int) $id . ',';		}

Thanks for your help,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...