ow-design Posted May 21, 2007 Share Posted May 21, 2007 Hello Everyone... A New problem... <?php include("db.php"); switch($_GET["action"]) { case "add_item": { AddItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "update_item": { UpdateItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "remove_item": { RemoveItem($_GET["id"]); ShowCart(); break; } default: { ShowCart(); } } function AddItem($itemId, $qty) { // Will check whether or not this item // already exists in the cart table. // If it does, the UpdateItem function // will be called instead global $dbServer, $dbUser, $dbPass, $dbName; // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); // Check if this item already exists in the users cart table $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); $row = mysql_fetch_row($result); $numRows = $row[0]; if($numRows == 0) { // This item doesn't exist in the users cart, // we will add it with an insert query @mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)"); } else { // This item already exists in the users cart, // we will update it instead UpdateItem($itemId, $qty); } } function UpdateItem($itemId, $qty) { // Updates the quantity of an item in the users cart. // If the qutnaity is zero, then RemoveItem will be // called instead global $dbServer, $dbUser, $dbPass, $dbName; // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); if($qty == 0) { // Remove the item from the users cart RemoveItem($itemId); } else { mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId"); } } function RemoveItem($itemId) { // Uses an SQL delete statement to remove an item from // the users cart global $dbServer, $dbUser, $dbPass, $dbName; // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); } function ShowCart() { // Gets each item from the cart table and display them in // a tabulated format, as well as a final total for the cart global $dbServer, $dbUser, $dbPass, $dbName; // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); $totalCost = 0; $result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc"); ?> <html> <head> <title> Your Shopping Cart </title> <script language="JavaScript"> function UpdateQty(item) { itemId = item.name; newQty = item.options[item.selectedIndex].text; document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty; } </script> </head> <body bgcolor="#ffffff"> <form name="frmCart" method="get"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <td width="8%" height="25" bgcolor="#755BCC"> <font face="verdana" size="1" color="white"> <b>Qty</b> </font> </td> <td width="62%" height="25" bgcolor="#755BCC"> <font face="verdana" size="1" color="white"> <b>Product</b> </font> </td> <td width="20%" height="25" bgcolor="#755BCC"> <font face="verdana" size="1" color="white"> <b>Price Each</b> </font> </td> <td width="10%" height="25" bgcolor="#755BCC"> <font face="verdana" size="1" color="white"> <b>Remove?</b> </font> </td> </tr> <?php while($row = mysql_fetch_array($result)) { // Increment the total cost of all items $totalCost += ($row["qty"] * $row["itemPrice"]); ?> <tr> <td width="8%" height="25"> <font face="Verdana" color="#755BCC" size="1"> <select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)" style="font-weight: 700"> <?php for($i = 1; $i <= 20; $i++) { echo "<option "; if($row["qty"] == $i) { echo " SELECTED "; } echo ">" . $i . "</option>"; } ?> </select><b> </b></font> </td> <td width="62%" height="25"> <font face="Verdana" size="1" color="#755BCC"> <b> <?php echo $row["itemName"]; ?> </b> </font> </td> <td width="20%" height="25"> <b><font face="Verdana" size="1" color="#755BCC">£ </font></b> </td> <td width="10%" height="25"> <b> <font face="Verdana" size="1" color="black"> <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>"> <font color="#755BCC">Remove</font></a></font><font face="Verdana" size="1" color="#755BCC"> </font> </b> </td> </tr> <?php } // Display the total ?> <tr> <td width="100%" colspan="4"> <hr size="1" color="#755BCC" NOSHADE> </td> </tr> <tr> <td width="70%" colspan="2"> <b><a href="index.php"> <font color="#755BCC" face="verdana" size="1"><< Keep Shopping </font></a></b> </td> <td width="30%" colspan="2"> <font face="verdana" size="2" color="#755BCC"> <b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?> </b> </font> </td> </tr> </table> </form> </body> </html> <?php } ?> With the cart code above how can i store all the products that are added to it in a session so i can use it in the checkout stage.? Thanks. Ollie Quote Link to comment https://forums.phpfreaks.com/topic/52340-shopping-cart-sessions/ 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.