Jump to content

how do i get to remove each item from this code


wixil

Recommended Posts

<?php 
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL);

define("PRODUCTIMAGE",0);
define("PRODUCTCODE", 1);
define("PRODUCTNAME", 2);
define("QUANTITY", 3);
define("PRICE", 4);

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   if (isset($_POST['productcode']))
   {
      AddToCart();
   }
   else
   {
      $action = isset($_POST['action']) ? $_POST['action'] : '';
      $value = strtoupper(substr($action, 0, 5));
      switch ($value)
      {
      // continue shopping
      case "CONTI":
         header("Location: "."products.html");
         break;

      // recalculate
      case "RECAL": 
         RecalculateCart();
         break;

      // proceed to checkout
      case "CHECK":
         header("Location: "."customer.php");
         break;
      } 
   }
} 


function AddToCart()
{
   $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
   $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

$productname = $_POST['productname'];
$extra_price = 0;
$productname = stripslashes($productname);

// Let's see if this product already exists
   for ($i=0; $i < $itemcount; $i++)
   {
     if ($cart[PRODUCTNAME][$i] == $productname) {
         $cart[QUANTITY][$i] = $cart[QUANTITY][$i] + intval($_POST['quantity']);
         $_SESSION['cart'] = $cart;
         $_SESSION['itemcount'] = $itemcount;
         header("Location: "."cart.php");
         exit;
   }
   }

   $cart[PRODUCTIMAGE][$itemcount] = $_POST['productimage'];
   $cart[PRODUCTCODE][$itemcount] = $_POST['productcode'];
   $cart[PRODUCTNAME][$itemcount] = $_POST['productname'];
   $cart[QUANTITY][$itemcount] = intval($_POST['quantity']);
   $cart[PRICE][$itemcount] = $_POST['price'];
   $itemcount = $itemcount + 1;

   $_SESSION['cart'] = $cart;
   $_SESSION['itemcount'] = $itemcount;
} 

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;
if($itemcount > 0){
   $total = 0;

	  }
else{
$total_quantity = 0;
$subtotal = 0;
$total = 0;
}

function RecalculateCart()
{
   $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
   $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

   for ($i=0; $i<$itemcount; $i++)
   {
      $quantity = $_POST['quantity'.($i)];
      if (empty($quantity))
      {
         $quantity = 0;
      }
      else
      if (($quantity < 0) || (!is_numeric($quantity)))
      {
         $quantity = 0;
      } 
      $cart[QUANTITY][$i] = intval($quantity);
   }

   for ($j=0; $j<$itemcount; $j++)
   {
      $quantity = $cart[QUANTITY][$j];

      // remove item from the cart
      if ($quantity == 0)
      {
         $itemcount--;
        
         $curitem = $j;

         while(($curitem+1) < count($cart[0]))         
         {
            for ($k=0; $k<4; $k++)
            {
               $cart[$k][$curitem] = $cart[$k][$curitem+1];
               $cart[$k][$curitem+1] = '';
            }
            $curitem++;
         } 
      } 
   }
   $_SESSION['itemcount'] = $itemcount;
   $_SESSION['cart'] = $cart;
} 

?>

And the cart display table is

<?php 

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : '';
$itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0;

$strHTML = "";

if ($itemcount == 0)
{
   $strHTML = "<h3>Your shopping cart is empty.</h3>";
}
else
{
   $strHTML = "<div style=\"overflow:auto; height=100%;\">"."\n";
   $strHTML .= "<table border=\"0\" cellpadding=\"9\" cellspacing=\"0\" width=\"100%\">"."\n";
   $strHTML .= "<tr>"."\n";
   $strHTML .= "<td style=padding-top:12px;padding-bottom:12px;background-color:#04AA6D;color:white;font-size:18px;font-family:TimeBurner;>Product</td>"."\n"; 
   $strHTML .= "<td style=padding-top:12px;padding-bottom:12px;background-color:#04AA6D;color:white;font-size:18px;font-family:TimeBurner;>Description</td>"."\n";
   $strHTML .= "<td style=padding-top:12px;padding-bottom:12px;background-color:#04AA6D;color:white;font-size:18px;font-family:TimeBurner;>Quantity</td>"."\n";
   $strHTML .= "<td style=padding-top:12px;padding-bottom:12px;background-color:#04AA6D;color:white;font-size:18px;font-family:TimeBurner;>Price</td>"."\n";
   $strHTML .= "<td style=padding-top:12px;padding-bottom:12px;background-color:#04AA6D;color:white;font-size:18px;font-family:TimeBurner;>Total</td></tr>"."\n";

   $total = 0;
   $total_quantity = 0;
   for ($i=0; $i<$itemcount; $i++)
   {
      $strHTML .= "<tr>"."\n";
      $strHTML .= "<td>".$cart[PRODUCTIMAGE][$i]."</td>"."\n";
      $strHTML .= "<td style=font-size:14px;font-family:TimeBurner;font-weight:bold;>".$cart[PRODUCTNAME][$i]."</td>"."\n";
      $strHTML .= "<td><input type=\"text\" name=\"quantity".($i)."\" value=\"".$cart[QUANTITY][$i]."\" size=\"1\" style=padding:5px;border-radius:2px;background:#ffffff;border-width:1px;border-style:solid;border-color:#ccc;></td>"."\n";
      $strHTML .= "<td style=font-size:14px;font-family:TimeBurner;font-weight:bold;>"."$".number_format($cart[PRICE][$i],2)."</td>"."\n";
      $strHTML .= "<td style=font-size:14px;font-family:TimeBurner;font-weight:bold;>"."$".number_format($cart[PRICE][$i]*$cart[QUANTITY][$i],2)."</td>"."\n";
      $strHTML .= "</tr>"."\n";
      $total_quantity = $total_quantity + $cart[QUANTITY][$i];
      $total = $total + ($cart[PRICE][$i]*$cart[QUANTITY][$i]);
   }

   $strHTML .= "<tr>"."\n";
   $strHTML .= "<td></td><td></td><td></td>"."\n";
   $strHTML .= "<td style=font-size:20px;font-family:TimeBurner;font-weight:bold;>TOTAL</td>"."\n";
   $strHTML .= "<td style=font-size:20px;font-family:TimeBurner;font-weight:bold;>"."$".number_format($total, 2)."</td>"."\n";
   $strHTML .= "</tr>"."\n";
   $strHTML .= "</table>"."\n";
   $strHTML .= "</div>"."\n";
} 
echo $strHTML;

?>

 

 

Link to comment
Share on other sites

That's really easy: put an image or &times; on the page where you want it to appear.

Unless you're talking about some amount of functionality which you haven't explained yet? Maybe there's a particular behavior you have in mind regarding this button that you should tell us about if you want help to do it?

Link to comment
Share on other sites

The architecture of what you have is not conducive to making the change you desire.  All the UI is serverside code, driven by session variables.  

To make something like this work, you would want to rearchitect it so that you use ajax calls to add or remove an item from the cart or change quantity.  

Your PHP scripts would then be solely responsible for manipulating the session state on the server.  In essence you want one of the types of javascript UI's that people use js frameworks like Vue and React to create.

You don't need to use a javascript framework to create something like what you want, but you certainly would need to rewrite your code to at least have separate markup, that you then manipulate based on javascript routines that get or change the data via ajax.

You basic ajax call would get the representation of the current state of the cart from the session, ideally in json format.  You write a javascript routine that takes this data and builds the cart entries using it.

Once you do this, your buttons make calls that add items, or delete them or change the quantity of an existing item.  In each case, the ajax call makes an api call to the server, which in turn changes the sessions accordingly.    Those calls then call to your routine that gets the state of the cart and renders it.

You really should have no PHP code that is creating html markup as a string, since that will all be managed in js.

Basically you need to go back to the drawing board, if it's important enough for you to make this works the way you want.  I will say that it would certainly be worth the effort, if you plan to do any maintenance or in general want a more responsive UI that doesn't require a form post in order to represent any change to the state or contents of the cart. 

Link to comment
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.