jay_bo Posted February 14, 2010 Share Posted February 14, 2010 I have created a shopping cart and the products that have been added to the cart get displayed in the shopping cart page using this code, which works fine. foreach($_SESSION['product'] as $prod) { if ($prod["qty"]!=0) { echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<br/>'; } } and i add the products for the products page by if ($_GET["add"]=="true")//This line gets the value and reads it { $_SESSION["cost"]=($_SESSION["cost"]+$_POST["price"]*$_POST["quanity"]); $_SESSION["products"]=$_SESSION["products"]+$_POST["quanity"]; $_SESSION["order"]=$_SESSION["order"].$_POST["id"].","; $_SESSION["product"][$_POST["id"]]["qty"]=$_SESSION["product"][$_POST["id"]]["qty"]+$_POST["quanity"]; header ('Location: products.php?cat=1'); } How would i be able to delete a product from my shopping cart page? Many thanks Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/ Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 when you add you are setting a group of session variables. To delete, you unset($_SESSION['whatever']; Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012253 Share on other sites More sharing options...
jay_bo Posted February 14, 2010 Author Share Posted February 14, 2010 when you add you are setting a group of session variables. To delete, you unset($_SESSION['whatever']; I tried that but it didnt seem to work....i used unset($prod["qty"]); would that be correct in my case? Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012254 Share on other sites More sharing options...
wildteen88 Posted February 14, 2010 Share Posted February 14, 2010 No you'll need to unset the actual $_SESSION variable. $prod will not be the same as $_SESSION. Where in your code are you trying to delete the product from your basket? Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012258 Share on other sites More sharing options...
jay_bo Posted February 14, 2010 Author Share Posted February 14, 2010 I'm trying to delete it from the checkout...so the user adds the products to the mini cart then clicks to go to checkout. Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012261 Share on other sites More sharing options...
jay_bo Posted February 14, 2010 Author Share Posted February 14, 2010 i'm sorry i just got what you actually ment..... im displaying the products with this code and a link to remove each one..... foreach($_SESSION['product'] as $prod) { if ($prod["qty"]!=0) { echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<a href="shopping-cart.php?delete=true">Remove</a>'.'<br/>'; } } Then my remove code is.... if ($_GET["delete"]=="true")//This line gets the value and reads it { unset($prod["qty"]); header ('Location: products.php?cat=1'); } Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012262 Share on other sites More sharing options...
wildteen88 Posted February 14, 2010 Share Posted February 14, 2010 You will want to send the product id in the link too so you delete the correct $_SESSION variable. I'd change this foreach($_SESSION['product'] as $prod) { if ($prod["qty"]!=0) { echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<a href="shopping-cart.php?delete=true">Remove</a>'.'<br/>'; } } to foreach($_SESSION['product'] as $prod_id => $prod) { if ($prod["qty"]!=0) { echo 'Quantity: '.$prod["qty"].' - '.$prod["title"].' - £'.($prod["qty"]*$prod["price"]).'<a href="shopping-cart.php?id='.$prod_id.'&delete=true">Remove</a>'.'<br/>'; } } Now when deleting the product use if (isset($_GET['id'], $_GET["delete"]) && is_numeric($_GET['id']) && $_GET['delete'] =="true")//This line gets the value and reads it { $prod_id = (int) $_GET['id']; unset($_SESSION["product"][$prod_id]); header ('Location: products.php?cat=1'); } Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012264 Share on other sites More sharing options...
jay_bo Posted February 14, 2010 Author Share Posted February 14, 2010 Thanks that seemed to do the trick, but it isnt removing it from the overal price. Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012265 Share on other sites More sharing options...
jay_bo Posted February 14, 2010 Author Share Posted February 14, 2010 Would be able to carry the price across aswel just like the id? Link to comment https://forums.phpfreaks.com/topic/192060-shopping-cart/#findComment-1012266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.