nblackwood Posted June 22, 2010 Share Posted June 22, 2010 Hey all. I have an issue with a shopping cart I developed. Everything works the way its supposed to, but when I try and create a function that counts the number of items in the cart, I can't get it to work properly. I want to say something like: "You have 0 items in your cart" I want to display this on most pages. I know how to use the count function, but I'm having troubles with it. Here is the code snippit for my cart. Any help is appreciated. <?php session_start(); ini_set('display_errors', 1); error_reporting(E_ALL); define("PRODUCTCODE", 0); define("PRODUCTNAME", 1); define("QUANTITY", 2); define("PRICE", 3); 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: "."index.php"); break; // recalculate case "RECAL": RecalculateCart(); break; // proceed to checkout case "CHECK": header("Location: "."checkout.php"); break; } } } function AddToCart() { $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : ''; $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0; $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; } ?> Link to comment https://forums.phpfreaks.com/topic/205515-shopping-cart-help/ Share on other sites More sharing options...
googlit Posted June 29, 2010 Share Posted June 29, 2010 Hi i use the following, which could be adapted for your needs: to echo "you have X items in your cart use the following: <?php // Include functions require_once('functions.php'); ?> //echo cart message <?php echo writeShoppingCart(); ?> you will need to include the following in your functions file: <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>Your basket is empty.</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 basket</a></p>'; } } ?> you will need to edit the variables to match your requirements, but should hopefully help you out... Link to comment https://forums.phpfreaks.com/topic/205515-shopping-cart-help/#findComment-1078881 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.