mikebyrne Posted April 5, 2008 Share Posted April 5, 2008 I've setup a working shopping cart but wanted to add a "Purchase" button that would reduce the ammount of items purchased from my field Stocklevel Cart.php <?php // Include MySQL class require_once('mysql.class.php'); // Include database connection require_once('global.inc.php'); // Include functions require_once('functions.inc.php'); // Start the session session_start(); // Process actions $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="styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>Please check quantities...</h1> <?php echo showCart(); ?> <p><a href="login.php">Back to shop...</a></p> </div> </body> </html> Functions.php <?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>'; } } 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 product WHERE ProductNo = '.$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>'.$ProductName.' by '.$Productinfo.'</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); } ?> Link to comment https://forums.phpfreaks.com/topic/99664-purchase-button-on-cart-that-reduces-stock-levels/ Share on other sites More sharing options...
mikebyrne Posted April 5, 2008 Author Share Posted April 5, 2008 My products table looks like this CREATE TABLE `product` ( `Producttype` varchar(4) collate latin1_general_ci default NULL, `ProductName` varchar(80) collate latin1_general_ci default NULL, `ProductNo` double(50,0) NOT NULL auto_increment, `Stockamount` decimal(5,0) default NULL, `Display` varchar(3) collate latin1_general_ci default NULL, `Description` varchar(1000) collate latin1_general_ci default NULL, `Price` decimal(6,2) default NULL, `Image` varchar(50) collate latin1_general_ci default NULL, `Imgae2` varchar(50) collate latin1_general_ci default NULL, PRIMARY KEY (`ProductNo`) ) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; Link to comment https://forums.phpfreaks.com/topic/99664-purchase-button-on-cart-that-reduces-stock-levels/#findComment-509985 Share on other sites More sharing options...
mikebyrne Posted April 5, 2008 Author Share Posted April 5, 2008 I assume I need a piece of sql around the purchase button something like: UPDATE $table SET stockamount = stockamount - 1 WHERE ProductNo = $id Just not sure how to code it correctly Link to comment https://forums.phpfreaks.com/topic/99664-purchase-button-on-cart-that-reduces-stock-levels/#findComment-510125 Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 What are you struggling with? What exactly is the problem? That query looks fine to me. Link to comment https://forums.phpfreaks.com/topic/99664-purchase-button-on-cart-that-reduces-stock-levels/#findComment-512237 Share on other sites More sharing options...
mikebyrne Posted April 8, 2008 Author Share Posted April 8, 2008 The query works its the setting up of the button on the form i cant get right Link to comment https://forums.phpfreaks.com/topic/99664-purchase-button-on-cart-that-reduces-stock-levels/#findComment-512277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.