phpfreaksFTW Posted September 23, 2009 Share Posted September 23, 2009 i am making a little shopping cart want to make a "add to cart page" where the details of what was just added to the cart are shown. The user will be able to proceed to checkout or continue shopping. Item Price Qty Subtotal Fight 50.00 1 $50.00 i have this code <?php $qty = -1; foreach($_SESSION['cart'] as $id => $value) ; if($id = $_GET['id']) { $qty = $value; } if($qty < 0 ) { echo "its 0"; } else ?> and some output <tr> <?php $product = find_product($id); ?> <td><?php echo "Id is -->",($id); ?></td> <?php echo "<br>"; ?> <td><?php echo "Title is -->", $product['tite']; ?></td> <?php echo "<br>"; ?> <?php echo "Price is -->"; ?> <td><?php echo number_format($product['price'],2); ?></td> <?php echo "<br>"; ?> <?php echo "Quanity is -->"; ?> <td><input type="text" size="2" maxlength="2" name="<?php echo $id ?>" value="<?php echo $qty; ?>" </td> <?php echo "<br>"; ?> <?php echo "Total price is -->"; ?> <td>$<?php echo number_format($product['price']* $qty, 2); ?></td> </tr> the output displays with 3 times added to the cart of the id of 1 Id is -->1 Title is -->Halo 3 Price is -->55.00 Quanity is -->3 Total price is -->$165.00 the array looks like this and everything is fine with 3 items in the cart with and id of 1. Array ( [1] => 3 ) i add another item to the cart (id 2) and it still works fine Id is -->2 Title is -->Fight Night Rd 3 Price is -->50.00 Quanity is -->1 Total price is -->$50.00 the array looks like this Array ( [1] => 3 [2] => 1 ) now i go back and add another item with an id of 1 to the cart and this is where it goes bad. Id is -->1 Title is -->Halo 3 Price is -->55.00 Quanity is -->1 Total price is -->$55.00 the id is right the title is right the price is right the quanity is wrong and the total price is wrong because the the quanity is wrong however the price amount is right. but my arrray says Array ( [1] => 4 [2] => 1 ) i can not get it to go back and read the first element in the array again. it is stuck reading the 1 of [2] =>1 and can not fix it without clearing the cookies of the browser. can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/175287-foreach-hell/ Share on other sites More sharing options...
Mark Baker Posted September 24, 2009 Share Posted September 24, 2009 if($id == $_GET['id']) { not if($id = $_GET['id']) { Quote Link to comment https://forums.phpfreaks.com/topic/175287-foreach-hell/#findComment-923977 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.