2013-07-16 48 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