Jump to content

Recommended Posts

I have the following code, that displays a message whether a users shopping cart contains items or not.

 

How do I add/delete items from the shopping cart, upon the appropriate product link being clicked? If a items is already in the cart, how do I display a message notifying the user?

 

  $cart = $_SESSION['cart'];
  if (!$cart)
  {
    echo "You have no items in your shopping cart!";
  }
  else
  {
    echo "You have items in your shopping cart";
  }

 

 

Link to comment
https://forums.phpfreaks.com/topic/78219-adddelete-items/
Share on other sites

Oops, sorry wrong code file, the following code is what I have so far. Is there a different way of adding the id number of the product into the array?  - As I don't 100% understand what is happening. How do I notify the user that "The Item already exists", if the cart already contains their selected item?

 

$cart = $_SESSION['cart'];
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
$_SESSION['cart'] = $cart;

 

How do I delete records from the shopping cart?

 

<?php
session_start();

function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}

$cart = $_SESSION['cart'];
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
$_SESSION['cart'] = $cart;




$total = 0;
echo '<table>';
$id = $_GET['id'];

mysql_select_db("MP3", $con);

$items = explode(',',$cart);
echo $cart;
foreach ($items as $item)

{
$result = mysql_query("SELECT * FROM books WHERE id = '$item'") or die("Your have an error because:<br />" . mysql_error());
while($row = mysql_fetch_array($result))
  {

echo '<tr>';
echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
echo '<td>'.$row['title'].' by '.$row['author'].'</td>';
echo '<td>£'.$row['price'].'</td>';
$total = $total + $row['price'];
echo '</tr>';
}
}
echo '</table>';
echo '<p>Grand total: £'.$total.'</p>';

 

Also, I have a how do I put the add and delete code into a switch statement? I have the following code.

 

switch ($action)
{
  case 'add':

            break;

  case 'delete':

               break;
}

Link to comment
https://forums.phpfreaks.com/topic/78219-adddelete-items/#findComment-395825
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.