Jump to content

Shipping cart - products with different colors


pedrolopes10

Recommended Posts

Hi.. i'm building my own shipping cart for a shop in PHP/MySQL. i saw many forums and i saw one who sugests to put the colors separeted by "+" and then separete with explode(), it works fine until when i add two equal products with the same ID, but with different colors it adds to the database with the same ID and then when i delete a product he deletes the two.

How can i turn it arround? I would like for them to be different produts in the shipping cart.

I would apreciate any sugestion

Thanks

Link to comment
Share on other sites

How can i turn it arround? I would like for them to be different produts in the shipping cart.

 

Turn it around, hmmm....

 

Maybe a little code to let us see exactly what's going on.

Link to comment
Share on other sites

I'm using MySQL 5.1.30 and PHP 5.2.8

the code i have in shipping cart.php to add the product to the cart:

<?php
if (isset($_GET['accao'])){
if ($_GET['accao'] == "add"){
	if (isset($_GET['id'])){
		if (is_numeric($_GET['id'])){

			$id_prod = addslashes(htmlentities($_GET['id']));
			$cor_car = addslashes($_POST["color"]);
			$tmnh_car = addslashes($_POST["size"]);

			$sql_cont = "SELECT COUNT(*) AS n_prod FROM carrinho WHERE id_produto = '$id_prod' AND color_carrinho = '$color_car' AND size_carrinho = '$size_car' AND sessao = '".session_id()."'";
			$exe_cont = mysql_query($sql_cont, $base) or die(mysql_error());
			$reg_cont = mysql_fetch_array($exe_cont, MYSQL_ASSOC);
			//por um update para adicionar as quantidades iguais

			if($reg_cont['n_prod'] == 0){
				$sql_prod = "SELECT * FROM produtos WHERE id_produto = '$id_prod'";
				$exe_prod = mysql_query($sql_prod, $base) or die(mysql_error());
				$num_prod = mysql_num_rows($exe_prod);
				if($num_prod > 0){						
					$reg_prod = mysql_fetch_array($exe_prod, MYSQL_ASSOC);

					$sql_add = "INSERT INTO carrinho (id_produto, nome_produto, preco_produto, qtd_produto, color_carrinho, size_carrinho, sessao)
								VALUES ('".$reg_prod['id_produto']."', '".$reg_prod['nome_produto']."', '".$reg_prod['preco_produto']."', '".$_POST["qtd"]."', '".$_POST["color"]."', '".$_POST["size"]."', '".session_id()."')";							
					$exe_add = mysql_query($sql_add, $base) or die(mysql_error());

				}
			}
		}
	}
}

and the piece of code to choose the colors

<?php
$color_list = explode("+", $color);
for ($i=0;$i<count($color_list);$i++)
{
?>
<option value="<?php echo $color_list[$i]; ?>"><? echo $color_list[$i]; ?></option>

 

 

Link to comment
Share on other sites

I'm confused...  I know the colors are coming from $color_list, but what exactly does that look like.  You're going to have to use another delimiter to separate the colors so they show up separately in the SELECT list, and have a different ID.

 

How is this list ($color_list) being generated?

Link to comment
Share on other sites

oh sorry i forgot

$sql_det = "SELECT * FROM produtos WHERE id_produto = '$id_prod'";
$exe_det = mysql_query($sql_det, $base) or die(mysql_error());

...
while ($reg_det = mysql_fetch_array($exe_det, MYSQL_ASSOC)){
$cor = $reg_det['color_produto'];
$size = $reg_det['size_produto'];

<form action="cart.php?accao=add&id=<?=$id_prod?>" method="post">
<select name="color">
<?php
$color_list = explode("+", $color);
for ($i=0;$i<count($color_list);$i++)
{
?>
<option value="<?php echo $color_list[$i]; ?>"><? echo $color_list[$i]; ?></option>
<?
}
?>
</select>

it's a form and in the database i got the product id, name and color (red+yellow+brown)
i didn't understand your sugest but something like an id to a color and then in the form to select the color I SELECT from table with the same name(for example)?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.