0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
phpnewbs
  • 77
  • 1
  • 7
  • 1
    Better show the real error message from php code,line, errortext – JustOnUnderMillions Mar 13 '17 at 15:02
  • @JustOnUnderMillions I edited it now – phpnewbs Mar 13 '17 at 15:06
  • where are the function `list` and `each` defined? – Tobias F. Mar 13 '17 at 15:10
  • One bug i can see is: At the `array_splice` line you add/replace with `array(array(` thing that is wrong should onyl `array(`, but not realy sure on that. The error message says clearly the the given array is not in the format `array("itemID" => $id, "name" => $name, "price" => $price, "category" => $category, "quantity" => $quantity)` – JustOnUnderMillions Mar 13 '17 at 15:10
  • 1
    @Tobias F. in php (core functions) – JustOnUnderMillions Mar 13 '17 at 15:10
  • ok, i didn't knew about this function, just thought it was something self-definded :) – Tobias F. Mar 13 '17 at 15:11
  • @JustOnUnderMillions I dont think there is an error there, and the tutorial explained that it's declaring an array, inside an array. Thats why it's `array(array(` – phpnewbs Mar 13 '17 at 15:16
  • But that part manipulates the array so that `list()` wont work anymore. Because if you do `list($a,$b,$c)=each($arr)` each $arr entry must be an array with 3 values! And that is not so in all cases in your code. So i thing it has to do with the array_splice part. – JustOnUnderMillions Mar 13 '17 at 15:18
  • mmmm, i have missed something here: Why do you thing that this will evert be true `$id == "itemID" ` , You code currently goes and puts via `list()` the value of `$eachItem[x]['itemID']` into the variable `$id`. So is there a case that this can be given `$eachItem[x]['itemID']='itemID'`?? Please explain. – JustOnUnderMillions Mar 13 '17 at 15:21

0 Answers0