CASHOUT Posted August 9, 2011 Share Posted August 9, 2011 ok so the cart runs, but when i add the same item to the shopping cart, instead of recording the same item id and making it a count of 2, it adds a new item_id.. notice the underscore. please help if you can cart.php <?php session_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php if (isset($_POST['pid'])){ $pid = $_POST['pid']; $wasFound = false; $i = 0; //if the cart session variable is not set or cart arraty is empty if (!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"]) < 1){ //RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(1 => array("item id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item){ $i++; while (list($key, $value) = each($each_item)){ if ($key == "item_id" && $value == $pid){ // That item is in cart already so lets adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; }// close if condition }// close while loop }// close foreach loop if ($wasFound == false){ array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 2 (if user chooses to empty their shopping cart //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart"){ unset($_SESSION["cart_array"]); } ?> <?php //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (render the cart for the user to view) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){ $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { $i = 0; foreach ($_SESSION["cart_array"] as $each_item){ $i++; $cartOutput .= "<h2>Cart item $i</h2>"; while (list($key, $value) = each($each_item)){ $cartOutput .= "$key: $value<br />"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Your Cart</title> <link href="style/style.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("template_header.php");?> <div id="pageContent"> <div style="margin:24px; text-align:left;"> <?php echo $cartOutput; ?> <br /><br /> <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a> </div> <br /> </div> <?php include_once("template_footer.php");?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244281-cartphp/ Share on other sites More sharing options...
CASHOUT Posted August 9, 2011 Author Share Posted August 9, 2011 Could the problem be in the array_splice syntax? Quote Link to comment https://forums.phpfreaks.com/topic/244281-cartphp/#findComment-1254660 Share on other sites More sharing options...
the182guy Posted August 9, 2011 Share Posted August 9, 2011 On this line you're using a space in item id instead of an underscore: $_SESSION["cart_array"] = array(1 => array("item id" => $pid, "quantity" => 1)); Quote Link to comment https://forums.phpfreaks.com/topic/244281-cartphp/#findComment-1254913 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.