2013-07-16 53 views
0

我得到一個未定義的:指數minicart錯誤中隔離會話變量

我有一個會話變量['cart_array']其存儲在多個陣列項目,我確定他們像

// If the cart session variable is not set or cart array is empty 
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
     // RUN IF THE CART IS EMPTY OR NOT SET 
     $_SESSION["cart_array"]["minicart"] = array(0 => array("item_id" => $pid, "quantity" => 1)); 

什麼是預防的最佳方法這個?

+1

有時你使用'$ _SESSION [「minicart」]'有時''_SESSION [「cart_array」] [「minicart」]'並且你寫了'mini_cart' ...你可能需要對此進行排序。在您創建的代碼中沒有任何地方設置了會話變量。 – Bun

+0

@Bun意外地刪除了一個'[「minicart」]',但我現在把它放在問題中... – Amy

回答

0

創建一個方法返回您的總價格,而不是存儲總價格$minicart

function getTotalPrice() 
{ 
    $total = 0; 
    foreach ($_SESSION["cart_array"] as $item) 
    { 
     $total += $item['price']; 
    } 
    return $total; 
} 

當然,取代$item['price']與任何你用來存儲項目價格。

+0

你說我不需要'minicart'to顯示整體價格,但我可以使用'cart_array'來顯示陣列中的物品數量以及總體價格?如果是這樣的話,我會回顯'cart_array'的價格和'count'來顯示數組會話中的項目數量? – Amy

+0

不,你回聲'getTotalPrice()'函數。看起來這裏的問題是PHP不理解。從你以前的問題,你需要用'$ price'替換上面我的答案中的$ item ['price']'。 –

+0

你的權利我的理解是不是最好的,謝謝你的幫助 – Amy