ballouta Posted March 13, 2009 Share Posted March 13, 2009 Hi, I have a good shopping cart session-based code, but it is missing a remove option for each item in the cart! <?php //this is the ADD to cart code, no problems if ($_POST['add']) { foreach ($_POST['qty'] as $k => $v) { // if the value is 0 or negative // don't bother changing the cart if ($v > 0) { //echo "V = $v and K = $k"; V = QTY and K = article $_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v; } } } ?> and this is the update code: <?php if ($_POST['update']) { foreach ($_POST['qty'] as $k => $v) { // if the value is empty, 0 or negative // don't bother changing the cart if ($v != "" && $v >= 0) { $_SESSION['cart'][$k] = $v; } } } ?> I tried to 'imitate' the update abd clear code and did the following: in the cart itself: <?php <input type ='hidden' size=2 type=text name=\"qtyy[" . $k . "]\"> <input type='submit' name='remove' value='Remove'> //and I added those lines those if ($_POST['remove']) { foreach ($_POST['qtyy'] as $k => $v) { echo "V==$v"; $_SESSION['cart'][$k] = 0; } } ?> The Shopping cart is of course didn;t work with my remove modification, could you please help me? Please note that If i update any item qty in the cart to zero, it disappears. Thank you so much Quote Link to comment Share on other sites More sharing options...
Silverado_NL Posted March 13, 2009 Share Posted March 13, 2009 hmm cant find anything wrong, are u getting any error messages? if not maybe turn on error handling in your script. can u catch the $_POST[remove] and $_POST[qtty] data at the start of the script? might be worth while to check,try to do a print_f($_POST); and see if the form is working. other than that i cant seem to find anything wrong, except for the key which is named qtyy instead of just qty.(think that's on purpose) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 13, 2009 Share Posted March 13, 2009 What I would do, is find what the item is, then do unset So something like this... <?php $id = $_POST['itemid']; // Get the item ID to remove if ($_POST['remove']){ foreach ($_SESSION['cart'] as $k => $v){ if ($v == $id){ unset($_SESSION['cart'][$k]); } } } ?> Quote Link to comment Share on other sites More sharing options...
ballouta Posted March 13, 2009 Author Share Posted March 13, 2009 Hi The Little Guy in this situation, what would be the code in teh cart form instead of those two lines? <input type ='hidden' size=2 type=text name=\"qtyy[" . $k . "]\"> <input type='submit' name='remove' value='Remove'> thank you Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 13, 2009 Share Posted March 13, 2009 I would use a check box. So... foreach($_SESSION['cart'] as $id){ echo 'Remove: <input type="checkbox" name="itemid[]" value="'.$id.'" /><br />'; } echo '<input type="submit" name="remove" value="Remove" />'; Then I actually would check like this: if (isset($_POST['remove'])){ foreach ($_POST['itemid'] as $id){ if (in_array($id, $_SESSION['cart'])){ unset($_SESSION['cart'][$id]); } } } Give it a try and let me/us know how it works (you might have to modify/tweak it some)! Quote Link to comment Share on other sites More sharing options...
ballouta Posted March 13, 2009 Author Share Posted March 13, 2009 I made this change in the code: <input type='checkbox' name='itemid[]' value='$k' /> since my code that displays the cart begins with this: <?php if (is_array($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $k => $v) { // only display items that have been selected // that is, quantities > 0 if ($v > 0) { //etc... <input type='checkbox' name='itemid[]' value='$k' /> ?> i ticked the check box and pressed remove but still in the cart! kindly help Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 13, 2009 Share Posted March 13, 2009 Use $v instead of $k See what happens... Quote Link to comment Share on other sites More sharing options...
ballouta Posted March 13, 2009 Author Share Posted March 13, 2009 still the item in the cart! Quote Link to comment Share on other sites More sharing options...
ballouta Posted March 13, 2009 Author Share Posted March 13, 2009 You may begin tracing the problem from here This is my final code in the shopping cart.php form: <?php //it begins like this and there is no problem in viewing the cart content if (is_array($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $k => $v) { //$k is the item SKu which is the primary key/ item code <input type='submit' name='remove' value='Remove'> <input type='hidden' name='re' value='$k' /> //etc... ?> if the above two lines are correct then I still need the loop the removes this item from the cart PLZ Note that the add to cart code is working and it's like this: <?php if ($_POST['update']) { foreach ($_POST['qty'] as $k => $v) { // if the value is empty, 0 or negative // don't bother changing the cart if ($v != "" && $v >= 0) { $_SESSION['cart'][$k] = $v; } } } ?> could you please help me Quote Link to comment Share on other sites More sharing options...
Silverado_NL Posted March 13, 2009 Share Posted March 13, 2009 is your html form ok? all tags complete and ok? and more important, is the form action target set to the php script ur working with? something like <FORM action="index.php" method="post"> Quote Link to comment Share on other sites More sharing options...
ballouta Posted March 14, 2009 Author Share Posted March 14, 2009 Hi I do NOT have any problem in my form, what makes sure of that is that the update QTY and Empty Cart buttons are working properly. <form action='<?=$_SERVER['PHP_SELF']?>' method='post'> Thank you Quote Link to comment Share on other sites More sharing options...
Mikedean Posted March 14, 2009 Share Posted March 14, 2009 I could be completely wrong with what I assumed you was doing/trying to do, however this code works for me. <?php session_start(); if ($_POST['update']) { foreach ($_POST['qty'] as $k => $v) { // if the value is empty, 0 or negative // don't bother changing the cart if ($v != "" && $v >= 0) { $_SESSION['cart'][$k] = $v; } } } //this is the ADD to cart code, no problems if ($_POST['add']) { foreach ($_POST['qty'] as $k => $v) { // if the value is 0 or negative // don't bother changing the cart if ($v > 0) { //echo "V = $v and K = $k"; V = QTY and K = article $_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v; } } } if ($_POST['remove']) { for( $i = 0; $i < count( $_POST['qty'] ); $i++ ) { unset( $_SESSION['cart'][$_POST['qty'][$i]] ); } } ?> <form method="post"> <input type="text" name="qty[1]" /> <input type="submit" name="add" value="Add" /> </form> <form method="post"> <?php if( isset( $_SESSION['cart'] ) ) foreach( $_SESSION['cart'] as $k => $v ) echo "<input type='text' name='qty[" . $k . "]' value='" . $v . "' />"; ?> <input type="submit" name="update" value="Update" /> </form> <form method="post"> <?php if( isset( $_SESSION['cart'] ) ) foreach( $_SESSION['cart'] as $k => $v ) echo "<input type='checkbox' name='qty' value='" . $k . "' />"; ?> <input type="submit" name="remove" value="Remove" /> </form> 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.