Jump to content

How do i get total quantity 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;
}


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 code 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;
   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 = $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;

?>

 

Edited by Barand
code tags
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.