0

Hey.

So i was working on a shopping cart that didn't focus much on having a database to support the information.

I have a snippet of code that checks to make sure there is no dublicates. if there is it just changes the quantity to the right one.

but if a user inputs a quantity in the quantity box on the other page where the add to cart button is it. adds duplicates and my code doesn't seem to catch it.

Below is the code in question.

// Break Current Session
		$current_cart = explode("|",$_SESSION['cart']);
	
		$length = count($current_cart);
		echo $length;
		
		foreach($current_cart as $x => $v)
		{
			$temp = explode(",",$current_cart[$x]);

			if($temp[0] == $type && $temp[1] == $size)
			{
			
				$temp[0] = $type;
				$temp[1] = $size;
				$temp[2] = (int)$quantity + (int)$temp[2];
				$temp[3] = number_format($price, 2, '.', ' ');
			
			
				$current_cart[$x] = implode(',',$temp);
			}
			else
			{
				$current_cart[$x+1] .= implode(",",$item);
			}
		}
	
	$new_cart = implode("|",$current_cart);
	
	$_SESSION['cart'] = $new_cart;
	}
	else
	{
		$_SESSION['cart'] = implode(",",$item);;
	}

Mostly works with SESSIONS and stuff

Having Help ASAP would be nice as this is important project to my client.

1
Contributor
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Indirect
0

I seem to be having problems with Implode. it does seem to want to do the entire array.

Any know how to fix this?

foreach($current_cart as $x => $v)
		{
			$temp = explode(",",$current_cart[$x]);
			if($temp[0] == $type && $temp[1] == $size)
			{
				$temp[0] = $type;
				$temp[1] = $size;
				$temp[2] = (int)$quantity + (int)$temp[2];
				$temp[3] = number_format($price, 2, '.', ' ');
			
				
				$current_cart[$x] = implode(',',$temp);
				echo "<br/>".$current_cart[$x]."<br/>";
			}
			else
			{
				$temp[0] = $type;
				$temp[1] = $size;
				$temp[2] = (int)$quantity ;
				$temp[3] = number_format($price, 2, '.', ' ');
				$current_cart[$x+1] .= implode(",",$item);
			}
		}
	
	$_SESSION['cart'] = implode("@",$current_cart);

thats my code

specialty boxes,Large Mirror Box,1,8.5@specialty boxes,China / Dish,1,7.5specialty boxes,Wardrobe Box,1,17.95@specialty boxes,Wardrobe Box,1,17.95

that is the contents of $_SESSION by the time in breaks you can see taht first and second element work fine but at the 3rd not so well.

Edited by Indirect: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.