Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 函数对数组中的第一项无效_Php_Arrays_Session_Shopping Cart - Fatal编程技术网

Php 函数对数组中的第一项无效

Php 函数对数组中的第一项无效,php,arrays,session,shopping-cart,Php,Arrays,Session,Shopping Cart,我有一个函数,可以更改产品的数量并将其存储回会话中 public static function updateProduct($product_code, $newQty) { foreach ($_SESSION["products"] as $cart_itm) //loop through session array { if($cart_itm["code"] == $product_code) //the item exist in array

我有一个函数,可以更改产品的数量并将其存储回会话中

public static function updateProduct($product_code, $newQty)
{   
    foreach ($_SESSION["products"] as $cart_itm) //loop through session array
    {
        if($cart_itm["code"] == $product_code) //the item exist in array
        {
            $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$newQty);
        }
        else
        {
            //item doesn't exist in the list, just retrieve old info and prepare array for session var
            $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"]);
        }
    }
    //found user item in array list, and increased the quantity
    $_SESSION["products"] = $product;
return;
}
但是,如果阵列中有两个或多个产品,则该函数仅适用于最后添加的产品。 有人有主意吗

PS:将产品添加到购物车的功能运行良好。但是没有什么大的区别

评论代码:

$cart_items = 0;

foreach ($_SESSION["products"] as $cart_itm)
{       
echo '<input type="hidden" name="product_code" value="'.$product_code.'" />';           
$cart_items ++;
}

if(isset($_SESSION["products"]))
{
    echo '<form method="post" name="updateProduct">';

    echo '<ul>';
    $cart_items = 0;

    foreach ($_SESSION["products"] as $cart_itm)
    {
       $product_code = $cart_itm["code"];
        // get the product_name
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select($db->quoteName('product_name'))
         ->from($db->quoteName('#__osmaf_products'))
         ->where($db->quoteName('product_code') . ' = '. $db->quote($product_code));
        $db->setQuery($query);
        $result = $db->loadRow();

        echo '<li class="cart-itm">';
        echo '<h4>'.$result['0'].' (Artikelnr.:'.$product_code.')</h4>';
        echo '<label style="display:inline">Menge: </label><input type="number" style="width:50px;margin-top:9px" name="newqty" value="'.$cart_itm["qty"].'" />';
        echo ' <input type="submit" class="btn btn-primary" name="updateProduct" value="&#x21bb;" />';
        echo '<input type="hidden" name="product_code" value="'.$cart_itm["code"].'" />';
        echo '</li>';
        $cart_items ++;

    }
}

当我现在每次发送表单时,即使我想更新哪个产品,最后一个产品的姓氏和数量也会发送到函数。

我对您的代码做了一些更改

public static function updateProduct($product_code, $newQty)
{   
    foreach ($_SESSION["products"] as $cart_item_id => $cart_itm) {
        if($cart_itm["code"] == $product_code) {
            $_SESSION["products"][$cart_item_id]["qty"] = $newQty;
        }

    }
}

试试看

我认为,通过这种方式,您不会在$product数组中添加项目,而是在每次重新启动时添加项目。我建议您改用。

为什么不简单地更新阵列,而不是复制所有内容

注意&这将使$cart_项目成为参考,而不是另一个副本

public static function updateProduct($product_code, $newQty)
{   
    foreach ($_SESSION["products"] as &$cart_itm) //loop through session array, but as reference
    {
        if($cart_itm["code"] == $product_code) //the item exist in array
        {
            $cart_item["qty"] = $newQty;
            //assuming $product_code unique, so we are done.
            return;
        }
    }
}

正如在注释中所讨论的,您正在使用相同的名称而不是数组将产品代码存储在隐藏的直通循环中。此外,$product_代码未定义。即使以前定义过,也会存储相同的值。因此,您必须将所有产品代码存储为数组,如下所示

foreach ($_SESSION["products"] as $cart_itm)
{       
echo '<input type="hidden" name="product_code[]" value="'.$cart_itm["code"].'" />';           
$cart_items ++;
}

请解释您更改了什么。您是否打印过r$产品;在分配给这个函数中的会话之前?我知道了!但它与函数没有任何关系。。。问题是产品代码:看这里,我得到了正确的产品代码。。。但是如果foreach循环是finish,那么产品代码就是上一个循环中的代码:/解决这个问题有什么想法吗?您尝试过下面的解决方案吗?请查看上面我添加的新代码片段。这段代码用于在隐藏中添加产品代码并传递给updateProduct函数?[]推送一个条目。数组只有在不存在时才会初始化。请查看主线程上的注释。价值就在那里。表单是问题所在。将表单放置在循环中的目的是什么?明白了。该表格应首先用于所有产品的内容,并用于更改一个产品的数量。现在完成。。。东西都在这个foreach中,所以foreach项目都会有这个表单,这样我就可以用post格式得到正确的产品编号。谢谢你的帮助。你把我带到了那一点;你找到解决办法了吗?是的,现在我有两个表格,一个在foreach中用于更改数量,另一个在外部用于另一个表格;