wixil Posted May 20, 2022 Share Posted May 20, 2022 <?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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/ Share on other sites More sharing options...
requinix Posted May 20, 2022 Share Posted May 20, 2022 How to do what from where? Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596490 Share on other sites More sharing options...
wixil Posted May 20, 2022 Author Share Posted May 20, 2022 i want to implement the circled option Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596492 Share on other sites More sharing options...
requinix Posted May 20, 2022 Share Posted May 20, 2022 That's really easy: put an image or × 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? Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596493 Share on other sites More sharing options...
wixil Posted May 20, 2022 Author Share Posted May 20, 2022 i can put an image or a times sign but i needed a call code that can actually remove an item when the image or times icon is clicked Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596495 Share on other sites More sharing options...
Barand Posted May 20, 2022 Share Posted May 20, 2022 It looks like you have an item quantity with a minus sign to the left and a plus sign to the right, presumably to amend the quantity. If the quantity is "1" and you click "minus", doesn't that have the effect you want - reduce it to zero? Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596496 Share on other sites More sharing options...
wixil Posted May 20, 2022 Author Share Posted May 20, 2022 if i reduce it to zero i will have to do a "recalculate" for it to reflect in the cart i needed this option so as to be able to remove item directly without clicking recalculate Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596497 Share on other sites More sharing options...
Barand Posted May 20, 2022 Share Posted May 20, 2022 If I were a customer, I wouldn't want to remove an item and then end up paying for it because the programmer couldn't be bothered to recalculate. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596498 Share on other sites More sharing options...
wixil Posted May 20, 2022 Author Share Posted May 20, 2022 The code i provided above is the preview of this image attached here, i used the first image to state what i want Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596499 Share on other sites More sharing options...
gizmola Posted May 20, 2022 Share Posted May 20, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314817-how-do-i-get-to-remove-each-item-from-this-code/#findComment-1596500 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.