2014-01-31 33 views
0

我有一个函数可以改变产品的数量并将其存储回会话中。函数对阵列中的第一项没有任何影响

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:将产品添加到购物车的功能正常。但是没有什么大的差别。

CODE的评论:

$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 ++; 

    } 
} 

当我现在送我想更新其产品的形式每次均匀,最后产品的姓氏和数量得到发送到功能。

+0

您是否在执行此函数中的'print_r($ product)'前分配了'print_r($ product);' –

+0

我懂了!但它没有做任何与功能...问题是产品代码:\t LOOK ABOVE \t这里我得到了正确的产品代码...但如果foreach循环完成产品代码是从代码最后一个:/任何想法来解决这个问题? –

+0

您是否尝试过以下解决方案? –

回答

0

我改了一下你的代码

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; 
     } 

    } 
} 

试试吧。

+0

请解释你改变了什么。 –

0

我认为这样你就不会在$ product数组中添加项目,而是每次重新启动它。我建议您改用array_push

+0

''''推入一个条目。数组只会在不存在的情况下被初始化。 – dognose

0

为什么不简单更新数组,而不是复制所有内容?

注意&这将使$cart_item引用,而不是另一个副本。

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; 
     } 
    } 
} 
0

正如评论中所讨论的那样,您将产品代码存储在具有相同名称(非数组)的隐藏直通循环中。还没有定义$product_code。即使在之前定义,也会存储相同的值。因此,您必须将所有产品代码存储为阵列,如下所示:

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

看看在主线程的评论。价值在那里。形式是问题。 –

+0

在循环中放置表单的目的是什么? –

+0

所以明白了。该表格应该首先用于所有产品和改变一种产品的数量。现在完整

...
事情是在这个foreach所以foreach项目会有这种形式,所以我可以得到正确的产品编号的帖子格式。谢谢你的帮助。你把我带到那一点;) –

相关问题