Jump to content

Basic Shopping Cart - delete function


meow2011

Recommended Posts

Hi guys,

 

I'm working on a basic shopping cart atm using sessions, and a text file rather than a database. I know it's not the best practice for a shopping cart (according to my three day google search) but I'm really determined to figure out where I am going wrong. I am trying to figure out how to remove an item from the shopping cart, I was "unsetting" the sessions, but it doesnt seem to work - also tried declaring the sessions as false but that didnt help (or I've done it the wrong way) I'm fairly new to php - so forgive me if it's something obvious I'm missing here.

 

Here is the information stored on the text file where I've stored the product info:

Gift Baskets:Chocolate Indulgence:A yummy collection of chocolates for the chocoholic:AUD 80:pbchoc.php:1

Gift Baskets:Coffee Indulgence:A yummy collection of coffees:AUD 40:pbcoffee.php:2

Meals:Cheese Platter:A delicious platter of cheeses:AUD 50:pmcheese.php:3

Meals:Coffee Cake:The perfect dessert:AUD 20:pmcake.php:4

 

Here is the base code for the product page:

<?php

 

if (isset($_POST['submit']))

{

 

  if($_SESSION['item']==$_POST['h1'])

{

  $_SESSION['qty'] = $_SESSION['qty'] + 1;

}

else

{

  $_SESSION['item'] = $_POST['h1'];

  $_SESSION['price']= $_POST['h2'];

}

 

$_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];

$_SESSION['itemqty'][$_SESSION['item']] = $_SESSION['qty'];

$_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];

 

}

 

?>

<table>

<tr>

<td>

<img src="images/choc.bmp"/>

</td>

<td> Unlike any other basket, this collection is a true testament to indulgence.

All of our favorite chocolate finds are here; cookies, brownies,and chocolate

bars are guaranteed to please even the most particular chocoholic.

Don't tell the dentist. Don't count the calories, just enjoy!..

</td>

<td>

<form action="pbchoc.php" method="post">

<p><input type="hidden" name="h1" value="Chocolate Basket"/>

<input type="hidden" name="h2" value="80"/>

<input type="submit" name="submit" value="Add to cart"/></p>

</form>

</td>

</tr>

</table>

 

<a href="cart.php">View shopping cart</a>

<br />

<br />

<form action="pcategory.php" method="post">

<p><input type="submit" name="back" value="Back"/></p>

</form>

 

 

AND Finally, the code for the cart page - where I'm having the trouble with deleting items from the cart:

<?php

 

 

  echo "<h2>My shopping Cart </h2>";

 

  if(isset($_POST['submit']))

  {

 

  $itemname = $_POST['h1'];

 

  //echo $_SESSION['itemname'][$itemname];

  unset($_SESSION['itemqty'][$itemname]);

  unset($_SESSION['itemprice'][$itemname]);

  unset($_SESSION['itemname'][$itemname]);

  }

 

  echo "<br/><br/>";

  echo "<table border='1'>";

  echo "<tr><th>Name</th><th>Quantity</th><th>Price</th></tr>";

 

  foreach($_SESSION['itemname'] as $key=>$value)

{

 

 

echo

'<tr><td>'.$_SESSION['itemname'][$key].'</td><td><input

type="text" name="t1" value='.$_SESSION['itemqty'][$key].'></td><td>'.$_SESSION['itemprice'][$key].'</td><td><form

id="f1" method="post" name="f1"><input type="submit" name="submit" value =

"delete"><input type="hidden" name="h1" value='.$key.'></td></tr>';

 

}

 

if(isset($_POST['submit']))

{

$_SESSION['itemname']--;

}

 

 

  echo "</table>";

 

?>

Thanks so much in advance to any who have the time to help! Really appreciate it!

Link to comment
https://forums.phpfreaks.com/topic/228180-basic-shopping-cart-delete-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.