chet139 Posted February 24, 2008 Share Posted February 24, 2008 Hi All, I may have no alternative but to post all the code so here it is - apologies. <?php require_once ('dbcon.html');//connect to db include("db.php"); session_start(); if (isset($_GET["action"])) { 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(); } } } else { ShowCart(); } function AddItem($productId, $qty) { // Will check whether or not this item // already exists in the cart table. // If it does, the UpdateItem function // will be called instead // Check if this item already exists in the users cart table $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and productId = $productId"); $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, productId, qty) values('" . GetCartId() . "', $productId, $qty)"); } else { // This item already exists in the users cart, // we will update it instead UpdateItem($productId, $qty); } } function UpdateItem($productId, $qty) { // Updates the quantity of an item in the users cart. // If the qutnaity is zero, then RemoveItem will be // called instead if($qty == 0) { // Remove the item from the users cart RemoveItem($productId); } else { mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and productId = $productId"); } } function RemoveItem($productId) { // Uses an SQL delete statement to remove an item from // the users cart mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and productId = $productId"); } 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 $totalCost = 0; $result = mysql_query("select * from cart inner join product on cart.productId = product.productId where cart.cookieId = '" . GetCartId() . "' order by product.productMake asc"); ?> <html> <head> <title>Order Contents </title> <script language="JavaScript"> function UpdateQty(product) { productId = product.name; newQty = product.options[product.selectedIndex].text; document.location.href = 'cart.php?action=update_item&id='+productId+'&qty='+newQty; } </script> </head> <body bgcolor="#ffffff"> <h1>Order Contents</h1> <form name="frmCart" method="get"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <td width="5%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Qty</b> </font> </td> <td width="5%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Product ID</b> </font> </td> <td width="5%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Product</b> </font> </td> <td width="5%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Price Each</b> </font> </td> <td width="5%" height="25" bgcolor="red"> <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["retailPrice"]); ****************$_SESSION['totalCost'] = $totalCost; ********************* ?> <tr> <td width="5%" height="25"> <font face="verdana" size="1" color="black"> <select name="<?php echo $row["productId"]; ?>" onChange="UpdateQty(this)"> <?php for($i = 1; $i <= 20; $i++) { echo "<option "; if($row["qty"] == $i) { echo " SELECTED "; } echo ">" . $i . "</option>"; } ?> </select> </font> </td> <td width="5%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["productId"]; ?> </font> </td> <td width="5%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["productMake"]; ?> </font> </td> <td width="5%" height="25"> <font face="verdana" size="1" color="black"> £<?php echo number_format($row["retailPrice"], 2, ".", ","); ?> </font> </td> <td width="5%" height="25"> <font face="verdana" size="1" color="black"> <a href="cart.php?action=remove_item&id=<?php echo $row["productId"]?>">Remove</a> </font> </td> </tr> <?php } // Display the total ?> <tr> <td width="100%" colspan="4"> <hr size="1" color="red" NOSHADE> </td> </tr> <tr> <td width="70%" colspan="2"> <font face="verdana" size="1" color="black"> <a href="getallProduct.php"><< Products List</a> </font> </td> <td width="70%" colspan="2"> <font face="verdana" size="1" color="black"> <a href="indOrder2.php?custId=<?php echo $_SESSION["custId"]?>&orderId=<?php echo $_SESSION["orderId"]?>&userId=<?php echo $_SESSION["userId"]?>"><< Order Page</a> </font> </td> <td width="30%" colspan="2"> <font face="verdana" size="2" color="black"> <b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?></b> </font> </td> </tr> </table> </form> <?php echo $_SESSION['totalCost'];//PUT IN TO SEE WHATS STORED IN THIS SESSION VAR?> </body> </html> <?php} ?> I am using this cart code - I attempted to store the total in the session variable(highlighted with ****) - however when the cart is emptied the session variable still holds the total. How can I fix this. The last PHP echo line is there for debugging purposes so I can see the contents of $_SESSION['totalCost']. Link to comment https://forums.phpfreaks.com/topic/92732-possible-session-issue/ Share on other sites More sharing options...
chet139 Posted February 24, 2008 Author Share Posted February 24, 2008 Please If this is unclear do ask. Basically the session is still holding the total even when everything is REMOVED from the cart db. I was thinking unset() or the session_destroy() but are they appropriate and where would I put them. Link to comment https://forums.phpfreaks.com/topic/92732-possible-session-issue/#findComment-475114 Share on other sites More sharing options...
paul2463 Posted February 24, 2008 Share Posted February 24, 2008 unset() destroys the named variable you point it at session_destroy() destroys the whole session and all variables held in it Link to comment https://forums.phpfreaks.com/topic/92732-possible-session-issue/#findComment-475127 Share on other sites More sharing options...
mem0ri Posted February 24, 2008 Share Posted February 24, 2008 I may have missed it (there was a long bit of code there)...but in the only location I see anything removed (removeItem), you remove only from the database...not from the session...you would actually need to remove from the session as well in order for the session variable to update. Link to comment https://forums.phpfreaks.com/topic/92732-possible-session-issue/#findComment-475131 Share on other sites More sharing options...
chet139 Posted February 24, 2008 Author Share Posted February 24, 2008 I probably need to use unset. Your right in that its only removed from database. Thats what I need to know - where do I remove it from the session where Do i Put it. I am new to this so confused Thanks Link to comment https://forums.phpfreaks.com/topic/92732-possible-session-issue/#findComment-475138 Share on other sites More sharing options...
chet139 Posted February 24, 2008 Author Share Posted February 24, 2008 Ok so i put unset($_SESSION['totalCost']); into the function RemoveItem. And it seems to be ok now. Thanks Link to comment https://forums.phpfreaks.com/topic/92732-possible-session-issue/#findComment-475142 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.