jayteepics Posted August 7, 2011 Author Share Posted August 7, 2011 The array indexes between the post and session arrays are not the same (spaces vs underscores), so the current logic is setting the session variables to nulls (non-existent post variables.) I'm going to guess this is because of some missing quotes in the form's html markup/php converting array index names to valid php variable names. Could you post the code that is generating the form? (edit: I guess that is the display_cart() function. I'll take a look at what it is doing....) <?php } function display_cart($cart, $change = true, $images = 1) { // display items in shopping cart // optionally allow changes (true or false) // optionally include images (1 - yes, 0 - no) echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\"> <form action=\"show_cart.php\" method=\"post\"> <tr><th colspan=\"".(1 + $images)."\" bgcolor=\"#e8e8e8\">Item</th> <th bgcolor=\"#C9C9C9\">Price</th> <th bgcolor=\"#C9C9C9\">Quantity</th> <th bgcolor=\"#C9C9C9\">Total</th> </tr>"; //display each item as a table row foreach ($cart as $title => $qty) { // $title = mysql_prep($title); $book = get_book_details($title); echo "<tr>"; if($images == true) { echo "<td align=\"left\">"; if (file_exists("images/".$image.".jpg")) { $size = GetImageSize("images/".$image.".jpg"); if(($size[0] > 0) && ($size[1] > 0)) { echo "<img src=\"images/".image.".jpg\" style=\"border: 1px solid black\" width=\"".($size[0]/3)."\" height=\"".($size[1]/3)."\"/>"; } } else { echo " "; } echo "</td>"; } echo "<td align=\"left\"> <a href=\"show_book.php?title=".$title."\">".$book['title']."</a> by ".$book['author']."</td> <td align=\"center\">£".number_format($book['price'], 2)."</td> <td align=\"center\">"; // if we allow changes, quantities are in text boxes if ($change == true) { echo "<input type=\"text\" name=\"".$title."\" value=\"".$qty."\" size=\"3\">"; } else { echo $qty; } echo "</td><td align=\"center\">£".number_format($book['price']*$qty,2)."</td></tr>\n"; } // display total row echo "<tr> <th colspan=\"".(2+$images)."\" bgcolor=\"#e8e8e8\"> </td> <th align=\"center\" bgcolor=\"#C9C9C9\">".$_SESSION['items']."</th> <th align=\"center\" bgcolor=\"#C9C9C9\"> £".number_format($_SESSION['total_price'], 2)." </th> </tr>"; // display save change button if($change == true) { echo "<tr> <td colspan=\"".(2+$images)."\"> </td> <td align=\"center\"> <input type=\"hidden\" name=\"save\" value=\"true\"/> <input type=\"image\" src=\"images/save-changes.gif\" border=\"0\" alt=\"Save Changes\"/> </td> <td> </td> </tr>"; } echo "</form></table>"; } function display_login_form() { // dispaly form asking for name and password ?> Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/page/2/#findComment-1253656 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2011 Share Posted August 7, 2011 The following changes (should) fix the problem. Change the following line (in the display_cart function) - echo "<input type=\"text\" name=\"".$title."\" value=\"".$qty."\" size=\"3\">"; To this - echo "<input type='text' name='arr[$title]' value='$qty' size='3'>"; Change - if ($_POST[$title] == '0') { To this - if ($_POST['arr'][$title] == '0') { Change - $_SESSION['cart'][$title] = $_POST[$title]; To this - $_SESSION['cart'][$title] = $_POST['arr'][$title]; Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/page/2/#findComment-1253657 Share on other sites More sharing options...
jayteepics Posted August 7, 2011 Author Share Posted August 7, 2011 Could you change the print_r debugging code to the following so that the different arrays are distinctly identified - echo '<pre>'; echo "Post:" print_r($_POST); echo "Session:"; print_r($_SESSION); echo '</pre>'; Also, what does a phpinfo(); statement show for the register_globals setting? It is set to OFF Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/page/2/#findComment-1253660 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2011 Share Posted August 7, 2011 I also see that your code has two (commented out) lines setting error_reporting and display_errors. Had those two lines been in effect, you would have been getting undefined notice messages when the non-existent $_POST variables were being accessed that would have directly called your's and our attention to where the problem was at. Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/page/2/#findComment-1253663 Share on other sites More sharing options...
jayteepics Posted August 7, 2011 Author Share Posted August 7, 2011 The following changes (should) fix the problem. Change the following line (in the display_cart function) - echo "<input type=\"text\" name=\"".$title."\" value=\"".$qty."\" size=\"3\">"; To this - echo "<input type='text' name='arr[$title]' value='$qty' size='3'>"; Change - if ($_POST[$title] == '0') { To this - if ($_POST['arr'][$title] == '0') { Change - $_SESSION['cart'][$title] = $_POST[$title]; To this - $_SESSION['cart'][$title] = $_POST['arr'][$title]; THAT DID IT!! It works now!! Here is the output... Post Array ( [arr] => Array ( [in Search of Unknown Britain] => 1 => 0 [The Jagged Nerves] => 1 ) [save] => true [x] => 55 [y] => 21 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => 1 [php and MySQL Web Development] => 1 [The Jagged Nerves] => 1 ) ) Post Array ( [arr] => Array ( [in Search of Unknown Britain] => 1 [php and MySQL Web Development] => 0 [The Jagged Nerves] => 1 ) [save] => true [x] => 55 [y] => 21 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => 1 [php and MySQL Web Development] => 1 [The Jagged Nerves] => 1 ) ) Post Array ( [arr] => Array ( [in Search of Unknown Britain] => 1 [php and MySQL Web Development] => 0 [The Jagged Nerves] => 1 ) [save] => true [x] => 55 [y] => 21 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => 1 [The Jagged Nerves] => 1 ) ) Post Array ( [arr] => Array ( [in Search of Unknown Britain] => 1 [php and MySQL Web Development] => 0 [The Jagged Nerves] => 1 ) [save] => true [x] => 55 [y] => 21 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => 1 [The Jagged Nerves] => 1 ) ) Total Items = 2 Total Price = £23.19 Your shopping cart Post Array ( [arr] => Array ( [in Search of Unknown Britain] => 1 [php and MySQL Web Development] => 0 [The Jagged Nerves] => 1 ) [save] => true [x] => 55 [y] => 21 ) Session Array ( [items] => 2 [total_price] => 23.19 [cart] => Array ( [in Search of Unknown Britain] => 1 [The Jagged Nerves] => 1 ) ) Item Price Quantity Total In Search of Unknown Britain by James Wellard £10.50 £10.50 The Jagged Nerves by Aldous Huxley £12.69 £12.69 2 £23.19 _____________________ I really can't thank you enough! And to all who helped. Unfortunately as a beginner with PHP there is no way I could see the wood for the trees. Thanks to your explanation I will go over this again to make sure that I actually have learnt something. Many Thanks Jamie Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/page/2/#findComment-1253670 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2011 Share Posted August 7, 2011 This problem is because php converts spaces and dots in post/get variable names (i.e. name="... ..." attributes) into underscores. Unfortunately, this is a side-affect of php's register_globals (where external and session variables were automatically converted into scaler program variables) that did and continues (even with register_globals turned off) to waste a huge amount of time. I can only guess that the script you are following was never tested with titles that had more than one word in them. Had it been, someone would have realized that an id value should have been used as the array keys instead of the title. Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/page/2/#findComment-1253680 Share on other sites More sharing options...
jayteepics Posted August 7, 2011 Author Share Posted August 7, 2011 The script probably did work with ISBN as the key....however below is my opening remark... "The project I am learning from is a shopping cart for books. Ordinarily, the supplied code would work IF all books have an ISBN. However books prior to some time in the seventies don't have an ISBN. So rather than devise a book code, with some trepidation, I used a book's 'title' as the DB primary key. Everything works except the removal of a book from the shopping cart i.e. setting the quantity on the form to '0'". Better not get two books with the same title eh? Many Thanks Again!! Jamie ps Do I somehow mark this problem as solved? Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/page/2/#findComment-1253709 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.