2014-01-31 37 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項目會有這種形式,所以我可以得到正確的產品編號的帖子格式。謝謝你的幫助。你把我帶到那一點;) –

相關問題