I am trying to make an e-commerce 'add to cart' function. I am VERY new to PHP, and I am in college and in my 3 years of studying I have only encountered multidimensional arrays now.
I understand what multidimensional arrays are it's just that I am very confused as how to implement it. I am following a tutorial for this one and my code is giving me an undefined offset error as well as looping the error over and over and over.
Notice: Undefined offset: 4 in C:\wamp\www\toResponsive\cart.php on line 59
this is line 59: while (list($id, $price, $name, $category, $quantity) = each($eachItem))
Notice: Undefined offset: 3 in C:\wamp\www\toResponsive\cart.php on line 97
and this is line 97 while (list($id, $price, $name, $category, $quantity) = each($eachItem)) -- same code but diff location. This error loops over and over with different offset numbers (Undefined offset: 1, 2 and so on .. 3, 4)
This happens when I click add to cart on one product:
if (isset($_POST['pID']) && ($_POST['pPrice']) && ($_POST['pName']) && ($_POST['pCategory']) && ($_POST['pQuantity'])) {
$id = $_POST['pID'];
$price = $_POST['pPrice'];
$name = $_POST['pName'];
$category = $_POST['pCategory'];
$quantity = $_POST['pQuantity'];
$wasFound = false;
$i = 0;
echo "Test";
//no cart session or cart is empty
if (!isset($_SESSION['cartArray']) || count($_SESSION['cartArray']) < 1) {
$_SESSION['cartArray'] = array(1 => array("itemID" => $id, "name" => $name, "price" => $price, "category" => $category, "quantity" => $quantity));
} else {
foreach ($_SESSION['cartArray'] as $eachItem) {
$i++;
while (list($id, $price, $name, $category, $quantity) = each($eachItem)) {
if ($id == "itemID" && $price == "price" && $name == "name" && $category == "category" && $quantity + 1) {
array_splice($_SESSION['cartArray'], $i-1, 1, array(array("itemID" => $id, "name" => $name, "price" => $price, "category" => $category, "quantity" => $quantity)));
$wasFound = true;
} // if
} // while
} //foreach
} //else
if ($wasFound == false) {
array_push($_SESSION['cartArray'], array("itemID" => $id, "name" => $name, "price" => $price, "category" => $category, "quantity" => $quantity));
} //second if
//} //root if
//rendering the cart items
if (!isset($_SESSION['cartArray']) || count($_SESSION['cartArray']) < 1) {
?> <!-- formatting empty cart header -->
<div class="cartTable">
<h2>There are no items in your cart</h2>
</div>
<?php //continuing the previous PHP script
} else {
?> <!-- closing to initialize the table headers -->
<div class="cartTable">
<table>
<tr>
<th>ITEM</th>
<th>PRODUCT</th>
<th>PRICE</th>
<th>CATEGORY</th>
<th>QTY</th>
</tr>
<?php //continuing after initializing table headers
$i = 0;
foreach ($_SESSION['cartArray'] as $eachItem) {
$i++;
while (list($id, $price, $name, $category, $quantity) = each($eachItem)) {
?> <!-- formatting the items -->
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $price; ?></td>
<td><?php echo $category; ?></td>
<td><?php echo $quantity; ?></td>
</tr>
<?php //continuing after formatting the items
} // while
}//foreach
}//else
?> <!-- closing the table and div -->
</table>
</div>
<?php
} //root if
?>
as you can see I am trying to loop the cart inside a table. I am getting the $_POST from a dynamic page called product.php from an <input type="hidden">
field