Foreach一遍又一遍地循环

时间:2017-03-13 15:00:07

标签: php arrays multidimensional-array mysqli

我正在努力使电子商务“添加到购物车”中。功能。我非常是PHP的新手,而且我在大学里,在我3年的学习期间,我现在只遇到了多维数组。

我理解多维数组是什么,只是因为我如何实现它而感到非常困惑。我正在按照这个教程进行操作,我的代码给了我一个未定义的偏移误差,以及一遍又一遍地循环错误。

  

注意:未定义的偏移量:C:\ wamp \ www \ toResponsive \ cart.php中的4   第59行

这是第59行:while(list($ id,$ price,$ name,$ category,$ quantity)= each($ eachItem))

  

注意:未定义的偏移量:C:\ wamp \ www \ toResponsive \ cart.php中的3   第97行

这是第97行而(list($ id,$ price,$ name,$ category,$ quantity)= each($ eachItem)) - 相同的代码但差异位置。此错误循环使用不同的偏移号(未定义的偏移量:1,2等等...... 3,4)

当我在一个产品上点击添加到购物车时会发生这种情况:

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
?>

正如您所看到的,我正在尝试将购物车循环到桌子内。我从<input type="hidden">字段中名为 product.php 的动态页面获取$ _POST

0 个答案:

没有答案