我的网站是职业追踪我想在我的网站上添加购物车,我每次都会出错,所以我累了。
我使用了XAMPP 1.8.1[PHP:5.4.7],每次都会出错注意:未定义的索引:第4行上的cart-in-functions.inc.php
我厌倦了为什么我的php veriable没有定义$推车
这是我的代码,我在第4行出错了。
php未定义索引错误
<?php
function writeShoppingCart()
{
$cart = $_SESSION['cart'];
if (!$cart)
{
return 'My Cart (0) Items';
}
else
{
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<a href="cart.php">'.count($items).' item'.$s.' in your cart</a></p>';
}
}
?>
您应该检查cart索引是否存在。
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
您的会话不包含任何名为"cart"的索引
要使会话在多个页面上可用,您需要在使用session_start
功能进行任何输出之前激活会话。
在设置变量之前访问它将引发一个通知。
请尝试先使用isset()
函数检查它是否存在
编辑后注意到您还没有开始会话:session_start()
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/function.session-start.php
只需将代码更改为:(在这种情况下,我通常使用)
<?php
function writeShoppingCart()
{
if(isset($_SESSION['cart']))
{
$cart = $_SESSION['cart'];
if (!$cart)
{
return 'My Cart (0) Items';
}
else
{
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<a href="cart.php">'.count($items).' item'.$s.' in your cart</a></p>';
}
}
}
?>
这能帮你吗。。。