Mr Chris Posted May 30, 2009 Share Posted May 30, 2009 Hi All, I have a simple dropdown menu on my page on a simple shopping cart script: <select name="qty" id="qty"> <option selected="selected" value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <input type="submit" value="Add to Cart" name="submit" /> and when I hit submit I print this value to the next page: print_r($_POST); Array ( [qty] => 4 [submit] => Add to Cart ) As you will see by testing my page here: http://www.inspireaway.co.uk/cart/ But nomatter how many qty I select from the first page only ONE item is always added to the cart. I know I have to somehow take qty from $_POST and somehow put it in my case statement for 'add' at this part, but i'm not sure how. Here's my code can anyone kindly help? cart.php <?php // Start the session session_start(); // Process actions print_r($_POST); $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo · Cart</title> <link rel="stylesheet" href="css/styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; } } echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>Please check quantities...</h1> <?php function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM books WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$title.' by '.$author.'</td>'; $output[] = '<td>£'.$price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>£'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } echo showCart(); ?> <p><a href="index.php">Back to bookshop...</a></p> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/160258-simple-shopping-cart-help/ Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Why do you do that? Why not just make $Quantity = mysql_escape_string($_POST['qty']); That's make things alot simpler to start with, rather than looping through your POST array to find the qty value Link to comment https://forums.phpfreaks.com/topic/160258-simple-shopping-cart-help/#findComment-845718 Share on other sites More sharing options...
Mr Chris Posted May 30, 2009 Author Share Posted May 30, 2009 Thank you. Sorry, but where would I put that in the code? Link to comment https://forums.phpfreaks.com/topic/160258-simple-shopping-cart-help/#findComment-845769 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 You tell me. At some point you exploded the $_POST variable to get the value of $_POST['qty'] Link to comment https://forums.phpfreaks.com/topic/160258-simple-shopping-cart-help/#findComment-845782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.