Jump to content

shopping cart issues


CASHOUT

Recommended Posts

what am i missing here???????

 

 

Notice: Undefined index: item_id in /home6/modeljew/public_html/cart.php on line 58

 

Notice: Undefined index: item_id in /home6/modeljew/public_html/cart.php on line 65

 

Notice: Undefined variable: product_name in /home6/modeljew/public_html/cart.php on line 67

 

Notice: Undefined variable: price in /home6/modeljew/public_html/cart.php on line 68

 

 

 

 

cart.php

<?php 
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php"; 
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 1 (if user attempts to add something to the cart from the product page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 on the page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$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++;
	$item_id = $each_item['item_id'];
	$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
	while ($row = mysql_fetch_array($sql)){
		$product_name = $row["product_name"];
		$price = $row["price"];
	}
	$cartOutput .= "<h2>Cart item $i</h2>";
	$cartOutput .= "item ID: " . $each_item['item_id']. "<br />";
	$cartOutput .= "Item Quantity: " . $each_item['quantity']. "<br />";
	$cartOutput .= "Item Name: " . $product_name . "<br />";
	$cartOutput .= "Item Price: " . $price . "<br />";
	//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
https://forums.phpfreaks.com/topic/244292-shopping-cart-issues/
Share on other sites

when you create the cart:

//RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(1 => array("item id" => $pid, "quantity" => 1));

 

you're using 'item id', but then you want to see it, you add an underscore: 'item_id'... that's why it says your variable is not defined. you're using the wrong name.

PHP Sensei

 

while ($row = mysql_fetch_array($sql)){			
$product_name = $row["product_name"];			
$price = $row["price"];		
}

 

this piece of code is so that, when an item is added to the cart, the product name and price is fetched from the mysql database and displayed on the shopping cart list

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.