Jump to content

cart.php


CASHOUT

Recommended Posts

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>

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.