jayteepics Posted August 6, 2011 Share Posted August 6, 2011 Nothing I have tried over the last 2 days has gotten anywhere near fixing this problem. I've been learning about shopping carts from the Welling & Thomson PHP and MySQL Web Dev. book (fourth ed.) 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'. This is snippets of the setup for the show_cart form. <form action=\"show_cart.php\" method=\"post\"> ... ... 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"; } ... ... 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>"; } I then run the scripts and choose a couple of books for my cart THEN make one of the quantity fields zero and execute the following in show_cart.php which is more or less identical to the script in the book however my key is $title as opposed to $isbn. if(isset($_POST['save'])) { foreach ($_SESSION['cart'] as $title => $qty) { if ($_POST[$title] == '0') { unset($_SESSION['cart']['$title']); } else { $_SESSION['cart'][$title] = $_POST[$title]; } } $_SESSION['total_price'] = calculate_price($_SESSION['cart']); $_SESSION['items'] = calculate_items($_SESSION['cart']); } The RESULT is all books disappear from the cart Any suggestions would be gratefully received. Jamie Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/ Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 use the CODE tags please.... Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253222 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 use the CODE tags please.... Could you expand a bit on what you mean please? Jamie Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253229 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 use the CODE tags please.... I can only guess you mean show the PHP tags so here if the function code..and the show_cart.php is attached. Jamie </table> <?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 ?> MOD EDIT: code tags added. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253305 Share on other sites More sharing options...
Pikachu2000 Posted August 6, 2011 Share Posted August 6, 2011 What he meant, even though he couldn't be bothered to provide any kind of example, is to enclose your code within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253306 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 What he meant, even though he couldn't be bothered to provide any kind of example, is to enclose your code within the forum's . . . BBCode tags. Pikachu2000. Thank You for your explanation. Does your assistance mean that my question is now 'properly formed' to be able to ask for assistance? I really still have to learn more about BBCode tags also....notwithstanding PHP, MySQL / Apache 2.2 on Win7. I'm an old mainframe guy and lecturer to boot just trying to learn new stuff and I was very surprised to be presented with hurdles just to ask for help. Notwithstanding this ignorance, if I have not provided, (even with your help), enough detail for my problem to be considered by the forum, then can someone help me phrase my problem appropriately to suit the protocol required please? Thank You for your help Pikachu2000 Jamie Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253384 Share on other sites More sharing options...
kickstart Posted August 6, 2011 Share Posted August 6, 2011 Hi Not sure but one problem is the following line:- unset($_SESSION['cart']['$title']) You have $title within single quotes so it is trying to unset an array member with the key of $title rather than the key with the value of $title. However I would expect that to mean nothing gets set to zero rather than the whole cart wiped out. All the best Keith (another ex mainframe programmer) Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253394 Share on other sites More sharing options...
Pikachu2000 Posted August 6, 2011 Share Posted August 6, 2011 The thing with using the . . . tags to enclose code is that your code is given syntax highlighting, making it easier for others to read through. The easier it is for people to help you, the more likely they'll actually do so. As for your question, To get a better idea of the data your form is passing, add the following lines near the top of the script that handles the form processing and post the output it generates. echo '<pre>'; print_r($_POST); echo '</pre>' Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253398 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 The thing with using the . . . tags to enclose code is that your code is given syntax highlighting, making it easier for others to read through. The easier it is for people to help you, the more likely they'll actually do so. As for your question, To get a better idea of the data your form is passing, add the following lines near the top of the script that handles the form processing and post the output it generates. echo '<pre>'; print_r($_POST); echo '</pre>' Pikachu2000, I'm all for getting my question focussed to the entire community and in that regard I very much appreciate your help. I added your code at the top of 'show_cart.php' as follows... Added ... to the top of show_cart.php <?php include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one session_start(); echo '<pre>'; print_r($_POST); echo '</pre>'; Chose 3 books for the cart and then changed the quantity of the 'PHP and Web Dev' book to zero and clicked 'Save Changes'... without removing the quotes around '$title' the result was... Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 67 [y] => 28 ) Total Items = 0 Total Price = £0.00 The result was ... Your shopping cart There are no items in your cart After removing the quotes around $title as per Keith's suggestion (Thanks Keith)... Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 49 [y] => 24 ) Total Items = 0 Total Price = £0.00 Your shopping cart There are no items in your cart Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253403 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 What he meant, even though he couldn't be bothered to provide any kind of example, is to enclose your code within the forum's . . . BBCode tags. I see, I wasn't clear enough, sorry OP.. edit: You are still not using the . . . Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253406 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 What he meant, even though he couldn't be bothered to provide any kind of example, is to enclose your code within the forum's . . . BBCode tags. I see, I wasn't clear enough, sorry OP.. edit: You are still not using the . . . OK here is the post again with the 3 lines of code I was recommended to add hopefully with the correct protocol this time. Many Thanks Jamie echo '<pre>'; print_r($_POST); echo '</pre>'; Chose 3 books for the cart and changed the quantity of PHP and Web Dev to zero and clicked 'Save Changes'... without removing the quotes around $title Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 67 [y] => 28 ) Total Items = 0 Total Price = £0.00 Your shopping cart There are no items in your cart After removing the quotes around $title as per Keith's suggestion... Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 49 [y] => 24 ) Total Items = 0 Total Price = £0.00 Your shopping cart There are no items in your cart Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253410 Share on other sites More sharing options...
kickstart Posted August 6, 2011 Share Posted August 6, 2011 Hi Can't see anything obvious (but not sure why you are using array_count_values). Could you try doing the following:- echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; Wondering if the session variables have been already wiped out, All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253420 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 Hi Can't see anything obvious (but not sure why you are using array_count_values). Could you try doing the following:- echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; Wondering if the session variables have been already wiped out, All the best Keith Hi Keith, Interesting result... echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; Result... Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 47 [y] => 22 ) Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => 1 => 1 [The Jagged Nerves] => 1 ) ) Total Items = 0 Total Price = £0.00 Your shopping cart There are no items in your cart .... Jamie Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253427 Share on other sites More sharing options...
kickstart Posted August 6, 2011 Share Posted August 6, 2011 Hi I would repeat the debugging that has been added and put it after each group of code to try and spot where $_SESSION is being cleared down All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253441 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 Hi I would repeat the debugging that has been added and put it after each group of code to try and spot where $_SESSION is being cleared down All the best Keith Here is the code now... <?php include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one session_start(); echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; //ini_set('display_errors', 1); //error_reporting(E_ALL); // // these lines format the output as HTML comments // and call dump_array repeatedly echo "\n<!-- BEGIN VARIABLE DUMP -->\n\n"; echo "<!-- BEGIN GET VARS -->\n"; echo "<!-- ".dump_array($_GET)." -->\n"; echo "<!-- BEGIN POST VARS -->\n"; echo "<!-- ".dump_array($_POST)." -->\n"; echo "<!-- BEGIN SESSION VARS -->\n"; echo "<!-- ".dump_array($_SESSION)." -->\n"; echo "<!-- BEGIN COOKIE VARS -->\n"; echo "<!-- ".dump_array($_COOKIE)." -->\n"; echo "\n<!-- END VARIABLE DUMP -->\n"; // dump_array() takes one array as a parameter // It iterates through that array, creating a single // line string to represent the array as a set //end-------------------- @$new = $_GET['new']; if($new) { //new item selected if(!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); $_SESSION['items'] = 0; $_SESSION['total_price'] ='0.00'; } if(isset($_SESSION['cart'][$new])) { $_SESSION['cart'][$new]++; } else { $_SESSION['cart'][$new] = 1; } $_SESSION['total_price'] = calculate_price($_SESSION['cart']); $_SESSION['items'] = calculate_items($_SESSION['cart']); } $saved_title = $title; if(isset($_POST['save'])) { foreach ($_SESSION['cart'] as $title => $qty) { if ($_POST[$title] == '0') { unset($_SESSION['cart']['$title']); echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; } else { $_SESSION['cart'][$title] = $_POST[$title]; echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; } } $_SESSION['total_price'] = calculate_price($_SESSION['cart']); $_SESSION['items'] = calculate_items($_SESSION['cart']); } do_html_header("Your shopping cart"); echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; if(($_SESSION['cart']) && (array_count_values($_SESSION['cart']))) { display_cart($_SESSION['cart']); } else { echo "<p>There are no items in your cart</p><hr/>"; } $target = "index.php"; // if we have just added an item to the cart, continue shopping in that category if($new) { $details = get_book_details($new); if($details['catid']) { $target = "show_cat.php?catid=".$details['catid']; } } display_button($target, "continue-shopping", "Continue Shopping"); // use this if SSL is set up // $path = $_SERVER['PHP_SELF']; // $server = $_SERVER['SERVER_NAME']; // $path = str_replace('show_cart.php', '', $path); // display_button("https://".$server.$path."checkout.php", // "go-to-checkout", "Go To Checkout"); // if no SSL use below code display_button("checkout.php", "go-to-checkout", "Go To Checkout"); do_html_footer(); ?> And rather wierd results at first glance.... Results...??? Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 39 [y] => 18) Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => 1 => 1 [The Jagged Nerves] => 1 ) ) Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 39 [y] => 18 ) Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => 1 [The Jagged Nerves] => 1 ) ) Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 39 [y] => 18 ) Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => [The Jagged Nerves] => 1 ) ) Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 39 [y] => 18 ) Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => [The Jagged Nerves] => ) ) Total Items = 0 Total Price = £0.00 Your shopping cart Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 39 [y] => 18 ) Array ( [items] => 0 [total_price] => 0.00 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => [The Jagged Nerves] => ) ) There are no items in your cart Many Thanks Jamie Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253455 Share on other sites More sharing options...
kickstart Posted August 6, 2011 Share Posted August 6, 2011 Hi If I am counting correctly I think that it has wiped out session somewhere in calculate_price() or calculate_items(). Can you post the code for those functions? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253460 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 Hi If I am counting correctly I think that it has wiped out session somewhere in calculate_price() or calculate_items(). Can you post the code for those functions? All the best Keith function calculate_price($cart) { // sum total price for all items in shopping cart $price = 0.0; if(is_array($cart)) { $conn = db_connect(); foreach($cart as $title => $qty) { $query = "select price from books where title='".$title."'"; $result = $conn->query($query); if ($result) { $item = $result->fetch_object(); $item_price = $item->price; $price +=$item_price*$qty; } } } return $price; } function calculate_items($cart) { // sum total items in shopping cart $items = 0; if(is_array($cart)) { foreach($cart as $title => $qty) { $items += $qty; } } return $items; } ?> Jamie Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253465 Share on other sites More sharing options...
kickstart Posted August 6, 2011 Share Posted August 6, 2011 Hi Afraid I am stumped. Double checked and it does look like the session variables have been wiped out in those functions but can't see where. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253469 Share on other sites More sharing options...
jayteepics Posted August 6, 2011 Author Share Posted August 6, 2011 Hi Afraid I am stumped. Double checked and it does look like the session variables have been wiped out in those functions but can't see where. All the best Keith Many Thanks Keith... just for completeness here is ... do_html_header($title = '') <?php function do_html_header($title = '') { // print an HTML header // declare the session variables we want access to inside the function if (!$_SESSION['items']) { $_SESSION['items'] = '0'; } if (!$_SESSION['total_price']) { $_SESSION['total_price'] = '0.00'; } ?> <html> <head> <title><?php echo $title; ?></title> <style> h2 { font-family: Arial, Helvetica, sans-serif; font-size: 22px; color: #664B4A; margin: 6px } body { font-family: Arial, Helvetica, sans-serif; font-size: 13px; color: #664B4A } li, td { font-family: Arial, Helvetica, sans-serif; font-size: 13px } hr { color: #FF0000; width=70%; text-align=center} a { color: #000000 } </style> </head> <body> <table width="100%" border="0" cellspacing="0" bgcolor="#e8e8e8"> <tr> <td rowspan="2"> <a href="index.php"><img src="images/cclogo.png" alt="CleverCollecting" border="0" align="left" valign="bottom" height="55" width="315"/></a> </td> <td align="right" valign="bottom"> <?php if(isset($_SESSION['admin_user'])) { echo " "; } else { echo "Total Items = ".$_SESSION['items']; } ?> </td> <td align="right" rowspan="2" width="135"> <?php if(isset($_SESSION['admin_user'])) { display_button('logout.php', 'log-out', 'Log Out'); } else { display_button('show_cart.php', 'view-cart', 'View Your Shopping Cart'); } ?> </tr> <tr> <td align="right" valign="top"> <?php if(isset($_SESSION['admin_user'])) { echo " "; } else { echo "Total Price = £".number_format($_SESSION['total_price'],2); } ?> </td> </tr> </table> <?php Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253471 Share on other sites More sharing options...
jayteepics Posted August 7, 2011 Author Share Posted August 7, 2011 From the previous output results, the only logical explanation for these results is the test for zero isn't working... if(isset($_POST['save'])) { foreach ($_SESSION['cart'] as $title => $qty) { if ($_POST[$title] == '0') { unset($_SESSION['cart'][$title]); echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; } else { $_SESSION['cart'][$title] = $_POST[$title]; echo '<pre>'; print_r($_POST); print_r($_SESSION); echo '</pre>'; } } As it progresses through the array systematically unsetting the session cart variables 1 then 2 then 3.. if ($_POST[$title] == '0') { unset($_SESSION['cart'][$title]); when it is only the second book which has a quantity of zero? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253629 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253634 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2011 Share Posted August 7, 2011 Edit to the above: After you change the print_r logic, repost the data that is displayed when you run your code. Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253645 Share on other sites More sharing options...
jayteepics Posted August 7, 2011 Author Share Posted August 7, 2011 Yes I already had to do that for myself ;-) Here is the output:- Post Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 57 [y] => 26 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => 1 => 1 [The Jagged Nerves] => 1 ) ) Post Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 57 [y] => 26 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => 1 [The Jagged Nerves] => 1 ) ) Post Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 57 [y] => 26 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => [The Jagged Nerves] => 1 ) ) Post Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 57 [y] => 26 ) Session Array ( [items] => 3 [total_price] => 55.18 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => [The Jagged Nerves] => ) ) Total Items = 0 Total Price = £0.00 Your shopping cart Post Array ( [in_Search_of_Unknown_Britain] => 1 [php_and_MySQL_Web_Development] => 0 [The_Jagged_Nerves] => 1 [save] => true [x] => 57 [y] => 26 ) Session Array ( [items] => 0 [total_price] => 0.00 [cart] => Array ( [in Search of Unknown Britain] => [php and MySQL Web Development] => [The Jagged Nerves] => ) ) There are no items in your cart Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253648 Share on other sites More sharing options...
PFMaBiSmAd Posted August 7, 2011 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....) Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253650 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. The method being used in the code you are trying to use is not very good. The name="" attributes should not be the $title. They should be an array with the index either being the $title or better yet, the id of the book. Quote Link to comment https://forums.phpfreaks.com/topic/244035-setting-a-zero-quantity-in-a-shopping-cart-problem/#findComment-1253653 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.