mr_jim Posted February 21, 2007 Share Posted February 21, 2007 Hi I am new to this forum wondered if anyone could help. I am creating a shopping basket and want to store three session variables (quantity, colour, size) against one product ID ($pid). I am trying to modify I page that I already had working that simply stored the quantity and product ID. Help please??? <?php include_once ('includes/header.html'); echo '<body>'; include ('includes/menu.html'); //check adding to cart if (is_numeric ($_POST['pid'])) { //These are the variables I am posting to the page and need to store against the SESSION[cart] array $pid= $_POST['pid']; $colour = $_POST['pid']; $size = $_POST['size']; echo "{$_SESSION['cart'][$pid]}"; //Check cart already contains one of these products if (isset ($_SESSION['cart'][$pid])) { $qty = $_SESSION['cart'][$pid] + 1; } else { $qty = 1; } //Add to the cart session variable $_SESSION['cart'][$pid] = $qty; } //Check if the form has been submitted (to update cart). if (isset ($_POST['submit'])) { foreach ($_POST['qty'] as $key => $value) { if (($value == 0) AND (is_numeric ($value))) { unset ($_SESSION['cart'][$key]); } elseif (is_numeric($value) AND ($value > 0) ) { $_SESSION['cart'][$key] = $value; } } } //Check if empty $empty = TRUE; if (isset ($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; } } } //Cart not empty if (!$empty) { require_once ('includes/config.inc'); require_once('includes/mysql_connect.php'); //Retrieve information $query = 'SELECT * FROM garments WHERE product_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query = substr ($query, 0, -1) . ') ORDER BY style'; echo $query; $result = mysql_query($query); //Create table & form echo '<table border="0" width="60%" cellspacing="3" cellpadding="3"> <tr> <td align="left" width="30%"><b><u>Product Name</u></b></td> <td align="left" width="10%"><b><u>Price</u></b></td> <td align="left" width="10%"><b><u>Qty</u></b></td> <td align="left" width="10%"><b><u>Total Price</u></b></td> </tr> <form action="view_cart.php" method="POST"> '; //Print each item $total = 0; //Total Cost of the order while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { //Calculate the total and subtotals. $subtotal = $_SESSION['cart'][$row['product_id']] * $row['price']; $total += $subtotal; //Print the row echo " <tr> <td align=\"left\">{$row['style']}</td> <td align=\"left\">{$row['price']}</td> <td align=\"left\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}] \" value=\"{$_SESSION['cart'][$row['product_id']]}\" /></td> <td align=\"left\">£" . number_format($subtotal, 2) . "</td>\n <td><td>{$row['product_id']}</tr>"; } //end while //Close table echo " <tr> <td colspan=\"3\" align=\"right\"><b>Total:</b></td> <td align=\"left\">£" . number_format($total, 2) . "</td>\n </tr> </table><div><input type=\"submit\" name=\"submit\" value=\"Update My Cart\"/></form>\n <form action=\"checkout.php\" method=\"POST\"> <input type=\"hidden\" name=\"total\" value=" . number_format($total, 2) ."> <input type=\"submit\" name=\"submit\" value=\"Checkout\"></form></div>"; mysql_close(); ?> <?php } else { echo '<p>Your cart is currently empty.</p>'; } include_once ('includes/footer.html'); ?> Link to comment https://forums.phpfreaks.com/topic/39496-first-shopping-cart/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.