2012-03-14 107 views
0

Heya我得到这个错误信息。我一直在寻找,但我不知道如何解决它?我是初学者,所以我真的很困惑。在此先感谢PHP未定义索引

注意:未定义指数:车在/nas/students/j/j39-green/unix/public_html/ISD5/inc/functions.inc.php第3行

你在没有任何项目您的购物车

<?php 
function writeShoppingCart() { 
$cart = $_SESSION['cart']; 
if (!$cart) { 
    return '<p>You have no items in your shopping cart</p>'; 
} else { 
    // Parse the cart session variable 
    $items = explode(',',$cart); 
    $s = (count($items) > 1) ? 's':''; 
    return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; 
} 
} 
+0

错误是在函数..Inc.php不是你的代码 – 2012-03-14 20:26:15

回答

1

如果$_SESSION['cart']没有设置,它抛出的警告。

尝试:

$cart = isset($_SESSION['cart'])?$_SESSION['cart']:false; 
+0

它的工作!谢谢! – user1269822 2012-03-14 20:38:01

0

这意味着,您的会话不包含$_SESSION['cart']

试试这个:

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : false; 
0

试试这个:

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : NULL; 
0

尝试像初始化您的$ _SESSION:

$_SESSION['cart'] = 0; // zero items in cart 
1

使用 “空”:

<?php 
function writeShoppingCart() { 
    $cart = !empty($_SESSION['cart']); 
    if (!$cart) { 
     return '<p>You have no items in your shopping cart</p>'; 
    } else { 
     // Parse the cart session variable 
     $items = explode(',',$cart); 
     $s = (count($items) > 1) ? 's':''; 
     return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your  shopping cart</a></p>'; 
    } 
} 
1

这意味着键 '车' 呢不存在于$ _SESSION超全局数组中。

如果不存在这个值这是正常的,你应该这样做:

$cart = false; 
if (isset($_SESSION['cart'])) { 
    $cart = $_SESSION['cart']; 
} 

你可以使用一种叫做三元运算符,在PHP,但因为你是一个初学者,我不不想轰炸你。

在开发模式下,可以显示错误,但您应该详细了解禁用错误(error_reporting)并将它们记录(error_log()),以便在不出现访问者的情况下对其进行检查。