pedrolopes10 Posted March 25, 2009 Share Posted March 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/151151-shipping-cart-products-with-different-colors/ Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151151-shipping-cart-products-with-different-colors/#findComment-794051 Share on other sites More sharing options...
pedrolopes10 Posted March 26, 2009 Author Share Posted March 26, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/151151-shipping-cart-products-with-different-colors/#findComment-794065 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/151151-shipping-cart-products-with-different-colors/#findComment-794073 Share on other sites More sharing options...
pedrolopes10 Posted March 26, 2009 Author Share Posted March 26, 2009 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)? Quote Link to comment https://forums.phpfreaks.com/topic/151151-shipping-cart-products-with-different-colors/#findComment-794081 Share on other sites More sharing options...
pedrolopes10 Posted March 26, 2009 Author Share Posted March 26, 2009 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)? Quote Link to comment https://forums.phpfreaks.com/topic/151151-shipping-cart-products-with-different-colors/#findComment-794094 Share on other sites More sharing options...
pedrolopes10 Posted March 27, 2009 Author Share Posted March 27, 2009 Hi... i'm sorry about the spam but i really need help... thanks Quote Link to comment https://forums.phpfreaks.com/topic/151151-shipping-cart-products-with-different-colors/#findComment-795302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.