2012-03-14 106 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()),以便在不出現訪問者的情況下對其進行檢查。