raptor30506090 Posted July 17, 2012 Share Posted July 17, 2012 Hi guys this is code for a t-shirt cart im working on but i keep getting two results from it when i first add a product after that seems to be ok? <?php if(isset($_POST['submit'])){ $productId = $_POST['id']; $quantity = $_POST['quantity']; $size = $_POST['size']; $colour = $_POST['colour']; $design = $_POST['design']; if(!isset($_SESSION['cart'][$productId])){ $_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'colour' => $colour, 'design' => $design)); } foreach ( $_SESSION['cart'][$productId] as &$product ) { if($product['design'] == $design && $product['colour'] == $colour && $product['size'] == $size ) { $product['quantity'] += $quantity; } } $_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'colour' => $colour, 'design' => $design); foreach($_SESSION['cart'][$productId] as $prod => $value){ echo $value['size'] .' '.$value['design']; }}?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 17, 2012 Share Posted July 17, 2012 It's hard to tell because of the messed up indenting, but you have this line " $_SESSION['cart'][$productId][] = array('quantity' =......." twice. Comment your code and figure out why it's twice. Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 17, 2012 Author Share Posted July 17, 2012 This is the help i already have been given this is with comment thanks <?php if(isset($_POST['submit'])){ $productId = $_POST['productId']; $quantity = $_POST['quantity']; $size = $_POST['size']; $color = $_POST['color']; $price = $_POST['price']; //if the productID doesn't exist, just create it: if ( !isset($_SESSION['cart'][$productId]) ) { $_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price)); } //product already exists, check to see if it's the same as another in the group: foreach ( $_SESSION['cart'][$productId] as &$product ) { //NOTE: If you want to override pricing with new tiers, you can remove 'price' from this comparison. You may also want to // fetch prices from the database when you DISPLAY the cart if you do bulk discounts if ( $product['price'] == $price && $product['color'] == $color && $product['size'] == $size ) { $product['quantity'] += $quantity; } } //if we've reached here, the productID is already in the cart but the current item is not a duplicate of an existing one: $_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price); foreach($_SESSION['cart'][$productId] as $prod => $value){ echo $value['size'] .' '.$value['price']; } //print_r($_SESSION['cart']); } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 17, 2012 Share Posted July 17, 2012 The comments don't match the logic. Did you write this code? Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 17, 2012 Share Posted July 17, 2012 Here it is with correct indenting. It's fairly obvious that it will create it twice. The "//if we've reached here" part will ALWAYS be "reached". <?php if(isset($_POST['submit'])){ $productId = $_POST['productId']; $quantity = $_POST['quantity']; $size = $_POST['size']; $color = $_POST['color']; $price = $_POST['price']; //if the productID doesn't exist, just create it: if ( !isset($_SESSION['cart'][$productId]) ) { $_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price)); } //product already exists, check to see if it's the same as another in the group: foreach ( $_SESSION['cart'][$productId] as &$product ) { //NOTE: If you want to override pricing with new tiers, you can remove 'price' from this comparison. You may also want to // fetch prices from the database when you DISPLAY the cart if you do bulk discounts if ( $product['price'] == $price && $product['color'] == $color && $product['size'] == $size ) { $product['quantity'] += $quantity; } } //if we've reached here, the productID is already in the cart but the current item is not a duplicate of an existing one: $_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price); foreach($_SESSION['cart'][$productId] as $prod => $value){ echo $value['size'] .' '.$value['price']; } //print_r($_SESSION['cart']); } ?> Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 17, 2012 Author Share Posted July 17, 2012 No i didnt write this code it was given me from here Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 18, 2012 Author Share Posted July 18, 2012 Still could do with some help question if im doing it so wrong can any one point me in the write root. what im trying to do is make a cart say t-shirt and have options for it like colour what they can select then if they choose same t-shirt but diffrent colour it add new product and dont over write last one any help would be great have been trying to do this for some time :'( Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 18, 2012 Share Posted July 18, 2012 I told you twice where the problem is.... Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 18, 2012 Author Share Posted July 18, 2012 ok then how do i go about fixing it i can only see it once like this $_SESSION['cart'][$productId][] = array('quantity' = Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 18, 2012 Author Share Posted July 18, 2012 i can only see it once like this with the extra bracket $_SESSION['cart'][$productId][] = array('quantity' = Quote Link to comment Share on other sites More sharing options...
ignace Posted July 18, 2012 Share Posted July 18, 2012 Because if you copied jesirose's code then it's already fixed. Obviously the double will only be in your code, not hers. Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 18, 2012 Share Posted July 18, 2012 Because if you copied jesirose's code then it's already fixed. Obviously the double will only be in your code, not hers. I'm not sure if that was supposed to be sarcasm, but I didn't fix the code, just the indenting, so it was readable. Legible? Whatever. I left the line I believe to be causing the problem in there. Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 18, 2012 Author Share Posted July 18, 2012 Na sorry still the same as before cant see any diffrence must admit been posting this for some time with out any luck cheers for any help :-\ Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 18, 2012 Share Posted July 18, 2012 You should write your own code rather than using code you don't understand. If you got this code here, and didn't understand it, you should have asked for an explanation. Write out the logic of what you're trying to do. Don't use the existing comments or code, just write down how it should work logically. Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 19, 2012 Author Share Posted July 19, 2012 Thank for your help i have now worked it out all working great Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 19, 2012 Author Share Posted July 19, 2012 :'( May be not solved i can put 3 items in the cart all works great then the four one it gos wrong doing my head in here is the new code <?php if(isset($_POST['submit'])){ $id = $_POST['id']; $sleeve = $_POST['sleeve']; $colour = $_POST['colour']; $size = $_POST['size']; $action = $_POST['action']; $quantity = 1; } //IF NOT IN CART if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity)); }else{ foreach($_SESSION['cart'] as $key => $value){ //IF IT IS IN THE CART IM CHECKING TO SEE IF SAME if($value['id'] == $id && $value['colour'] == $colour && $value['size'] == $size){ // ADDING ONE TO THE CART IF SAME $quantity = $value['quantity']+=1; $_SESSION['cart'] = array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity)); }else{ // IF NOT THE SAME PUTTING THIS OUT $_SESSION['cart'] []= array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity)); } } } foreach($_SESSION['cart'] as $cart => $value){ foreach($value as $c => $v){ echo $v; } } ?> Any help work be ace Quote Link to comment Share on other sites More sharing options...
raptor30506090 Posted July 19, 2012 Author Share Posted July 19, 2012 hi guys this seems to be a good one i thnik the problem is here <?php $_SESSION['cart'][] = array(array('id' => $id, 'colour' => $colour, 'size' => $size, 'quantity' => $quantity)); ?> how do i show a session with out the square brackets at the end of $_SESSION['cart'] Quote Link to comment 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.