I get these errors when I run the code and I know its pulling from product page NOT product page 2. Everything goes to the shopping cart from product page 2 with its coinciding Item_Id_2 but not product page and its coinciding Item_Id.
Any help will be highly appreciated!!!!
Notice: Undefined variable: price in C:\xampp\htdocs\onlinestore\cart2.php on line 136
Notice: Undefined variable: price in C:\xampp\htdocs\onlinestore\cart2.php on line 141
Notice: Undefined variable: item_id in C:\xampp\htdocs\onlinestore\cart2.php on line 146
Notice: Undefined variable: product_name in C:\xampp\htdocs\onlinestore\cart2.php on line 146
Notice: Undefined variable: product_name in C:\xampp\htdocs\onlinestore\cart2.php on line 147
Notice: Undefined variable: details in C:\xampp\htdocs\onlinestore\cart2.php on line 149
Notice: Undefined variable: price in C:\xampp\htdocs\onlinestore\cart2.php on line 150
Code: Select all
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section (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 array 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(0 => array("item_id" => $pid, "item_id_2" => $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" && "item_id_2" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "item_id_2" => $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, "item_id_2" => $pid, "quantity" => 1));
}
}
header("location: cart2.php");
exit();
}
?>
<?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 (if user chooses to adjust their item quantity)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
//execute code
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
If($quantity >=100){$quantity = 99;}
If($quantity < 1) {$quantity = 1;}
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && "item_id_2" && $value == $item_to_adjust) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id", "item_id_2" => $item_to_adjust, "quantity" => $quantity)));
} // close if condition
} // close while loop
} // close foreach loop
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 4 (if user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1) {
unset($_SESSION["cart_array"]);
} else {
unset($_SESSION["cart_array"]["$key_to_remove"]);
sort($_SESSION["cart_array"]);
}
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 5 (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
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) {
$item_id_2 = $each_item ['item_id_2'];
$sql = mysql_query("SELECT * FROM shirts WHERE id_2 = '$item_id_2' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row ["price"];
$details = $row ["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
$pricetotal = number_format($price * $each_item['quantity'],2);
$cartTotal = number_format($cartTotal,2);
//Dynamic table row assembly
$cartOutput.= '<tr>';
$cartOutput.= '<td><a href = "product2.php?id=' . $item_id_2 . '">' . $product_name . '</a><br/>
<img src ="inventory_images/' . $item_id_2 . '.jpg"alt="' . $product_name . '"width="40"height="52"border="1"/> </td>';
$cartOutput.= '<td>'.$details. '</td>';
$cartOutput.= '<td>$'.$price. '</td>';
$cartOutput.= '<td><form action = "cart2.php" method = "post">
<input name = "quantity" type = "text" value = "' . $each_item['quantity'] . '" size = "1" maxlength = "2" />
<input name = "adjustBtn' .$item_id_2. '" type = "submit" value = "Update" />
<input name = "item_to_adjust" type = "hidden" value = "' .$item_id_2. '"/>
</form></td>';
$cartOutput.= '<td>$' . $pricetotal . '</td>';
$cartOutput.= '<td><form action = "cart2.php" method = "post"><input name = "deleteBtn' . $item_id_2 .'" type = "submit" value = "Remove"/><input name = "index_to_remove"
type = "hidden" value = "' . $i . '" /></form></td>';
$cartOutput.= '</tr>';
$i++;
}
$cartTotal = "<div align = 'right'>Cart Total: $".$cartTotal." USD </div>";
}
?>