wixil Posted May 4, 2022 Share Posted May 4, 2022 (edited) <?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 May 4, 2022 by Barand code tags Quote Link to comment https://forums.phpfreaks.com/topic/314760-how-do-i-get-total-quantity-from-this-code/ Share on other sites More sharing options...
Barand Posted May 4, 2022 Share Posted May 4, 2022 Use the <> button in the toolbar when posting code. I have done it for you - this time. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314760-how-do-i-get-total-quantity-from-this-code/#findComment-1595940 Share on other sites More sharing options...
Barand Posted May 4, 2022 Share Posted May 4, 2022 Does this do it? Total_qty = array_sum($cart['QUANTITY']); Â 1 Quote Link to comment https://forums.phpfreaks.com/topic/314760-how-do-i-get-total-quantity-from-this-code/#findComment-1595942 Share on other sites More sharing options...
wixil Posted May 4, 2022 Author Share Posted May 4, 2022 Thanks @ Barand I am a somewhat php and js beginner. i needed to implement something like the attached png file probably by echo. Quote Link to comment https://forums.phpfreaks.com/topic/314760-how-do-i-get-total-quantity-from-this-code/#findComment-1595943 Share on other sites More sharing options...
Barand Posted May 4, 2022 Share Posted May 4, 2022 As you show a currency symbol, are you sure you don't want the total price? Quote Link to comment https://forums.phpfreaks.com/topic/314760-how-do-i-get-total-quantity-from-this-code/#findComment-1595944 Share on other sites More sharing options...
Barand Posted May 4, 2022 Share Posted May 4, 2022 $total_price = array_sum($cart['PRICE']); echo "₦ $total_price"; Gives something like Use FontAwesome or similar for the cart icon. Quote Link to comment https://forums.phpfreaks.com/topic/314760-how-do-i-get-total-quantity-from-this-code/#findComment-1595945 Share on other sites More sharing options...
wixil Posted May 5, 2022 Author Share Posted May 5, 2022 Sorted Quote Link to comment https://forums.phpfreaks.com/topic/314760-how-do-i-get-total-quantity-from-this-code/#findComment-1595953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.