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.

NVM Fixed it somehow. IDK how though

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.