Re: More on removing item from cart

From: Date: Wed, 26 Jun 2002 02:15:24 +0000
Subject: Re: More on removing item from cart
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-103885@lists.php.net to get a copy of this message
Richard, I did think about replacing the $cart array with $checkbox, but there were too many other uses of the variables for that to work cleanly. I've included below some relevant code. First is the function used to display the shopping cart. This includes the form with the checkbox. Then I've included what I believe is the most relevant part of the code on the receiving page, which is intended to loop through the array and unset a cart element when the relevant checkbox isn't checked - and when the save changes button is clicked. This code actually does remove the item from the cart BUT only after clicking the save button twice. I can't for the life of me figure out why it takes two clicks. Any ideas? Dan, I do understand your point of not actually removing items from the cart, but just displaying the relevant items, and I worked with the code you sent earlier for a long time. In fact, your method still makes the most sense to me. The main problem I ran into was combining the cart and checkbox info, which were in two different arrays. Maybe this will be clearer from the code posted below. Erik, I appreciate your comments and do understand that my logic has been faulty. However, I am hopeful that this is due to ignorance, which can be remedied, rather than stupidity, which can't. ;=) I really do appreciate the time you have all taken to read my questions and respond. Thanks, Vicki function display_cart($cart) { global $article_qty; //total number of articles in cart global $total_words; //total # words for all articles in cart //begin table echo "<table border = 0 width = 100% cellspacing = 0> <form action = show_cart.php method = post> <tr> <th bgcolor=\"#cccccc\">&nbsp;</th> <th align=left bgcolor=\"#cccccc\">Article</th> <th bgcolor=\"#cccccc\">Word Count</th></tr>"; //display each article on a row foreach ($cart as $artID => $headline) { $article = get_article_details($artID); $sum_words += $article[numwords]; $total_words = number_format($sum_words); $article_qty = count($cart); echo "<tr>"; echo "<td width=5% align = left bgcolor=\"#f4f4ea\"> <input type = checkbox name = \"$artID\" value = yes checked></td>"; echo "<td align = left> <a href=\"show_article.php?artID=".$artID."\">".$article["headline"]."</a></ td>\n"; echo "<td align = center>$article[numwords]</td>"; } //summary row and save changes button echo "<tr> <th bgcolor=\"#cccccc\">&nbsp;</th> <th align=left bgcolor=\"#cccccc\">Number of Articles = $article_qty</th> <th align = center bgcolor=\"#cccccc\">$total_words</th></tr> <tr> <td colspan=3 align = center><br> To remove an article from your order,<br> uncheck the box to the left of the title and click on the \"Save Changes\" button. <br><br> <input type = hidden name = save value = true> <input type = image src = \"images/save-changes.gif\" border = 0 alt = \"Save Changes\"></td></tr></form></table>"; } //relevant code from receiving page "show_cart.php" if($save) { foreach($cart as $artID => $value) { if(empty($value)) unset($cart[$artID]); else $cart[$artID] = $$artID; } }

« previous php.general (#103885) next »