PHP中的购物车无法正常工作
我一直在努力使用一个购物车的PHP代码 特别是,该功能可以以一种方式添加项目,从而可以轻松获得具有多个数量订单的项目 以下是我所拥有的,但添加第二项似乎不起作用:PHP中的购物车无法正常工作,php,session,cart,shopping,Php,Session,Cart,Shopping,我一直在努力使用一个购物车的PHP代码 特别是,该功能可以以一种方式添加项目,从而可以轻松获得具有多个数量订单的项目 以下是我所拥有的,但添加第二项似乎不起作用: function addtocart($pid,$q) { if($pid<1 or $q<1) return; if (is_array($_SESSION['cart'])) { $max=count($_SESSION['
function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;
if (is_array($_SESSION['cart']))
{
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['itemId']=$pid;
$_SESSION['cart']['itemId']['qty']= $_SESSION['cart']['itemId']['qty'] + $q;
$max=count($_SESSION['cart']);
echo "SECOND";
}
else
{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['itemId']=$pid;
$_SESSION['cart'][0]['qty'] = $q;
$max=count($_SESSION['cart']);
}
}
函数addtocart($pid,$q)
{
如果($pid您的函数可以简化为这样的函数
function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;
//if the cart is not defined, define it
if (!is_array($_SESSION['cart'])) $_SESSION['cart']=array();
//See if an arry item for the part number exists, or add it
if (!$_SESSION['cart'][$pid])
$_SESSION['cart'][$pid]=$q;
else
$_SESSION['cart'][$pid]+=$q;
}
这种方法很好,因为如果您添加两次相同的项目,数量将被添加到一起您的功能可以简化为这样
function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;
//if the cart is not defined, define it
if (!is_array($_SESSION['cart'])) $_SESSION['cart']=array();
//See if an arry item for the part number exists, or add it
if (!$_SESSION['cart'][$pid])
$_SESSION['cart'][$pid]=$q;
else
$_SESSION['cart'][$pid]+=$q;
}
这种方法很好,因为如果您添加两次相同的项目,数量将被添加到一起,这太复杂了
function addtocart($pid,$q) {
if($pid<1 or $q<1) return;
// has the main 'cart' array been set up in session yet? If not, do it
if (!array_key_exists('cart', $_SESSION) || !is_array($_SESSION['cart']))
$_SESSION['cart']=array();
// has this item been added to the cart yet? If not, do it (0 qty)
if (!array_key_exists($pid, $_SESSION['cart'])) {
$_SESSION['cart'][$pid] = array(
'itemId'=>$pid,
'qty'=>0
);
}
// add the requested quantity of items to the cart
$_SESSION['cart'][$pid]['qty'] = $_SESSION['cart'][$pid]['qty'] + $q;
}
文档
是数组
-
array\u key\u存在
-
你把事情复杂化了
function addtocart($pid,$q) {
if($pid<1 or $q<1) return;
// has the main 'cart' array been set up in session yet? If not, do it
if (!array_key_exists('cart', $_SESSION) || !is_array($_SESSION['cart']))
$_SESSION['cart']=array();
// has this item been added to the cart yet? If not, do it (0 qty)
if (!array_key_exists($pid, $_SESSION['cart'])) {
$_SESSION['cart'][$pid] = array(
'itemId'=>$pid,
'qty'=>0
);
}
// add the requested quantity of items to the cart
$_SESSION['cart'][$pid]['qty'] = $_SESSION['cart'][$pid]['qty'] + $q;
}
文档
是数组
-
array\u key\u存在
-
也许这对你有帮助。第二个索引应该是
$max
,而不是'itemId'
function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;
if (is_array($_SESSION['cart']))
{
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['itemId']=$pid;
$_SESSION['cart'][$max]['qty']= $_SESSION['cart'][$max]['qty'] + $q;
$max=count($_SESSION['cart']);
echo "SECOND";
}
else
{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['itemId']=$pid;
$_SESSION['cart'][0]['qty'] = $q;
$max=count($_SESSION['cart']);
}
}
函数addtocart($pid,$q)
{
如果($pid可能会对您有所帮助。第二个索引应该是$max
,而不是'itemId'
function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;
if (is_array($_SESSION['cart']))
{
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['itemId']=$pid;
$_SESSION['cart'][$max]['qty']= $_SESSION['cart'][$max]['qty'] + $q;
$max=count($_SESSION['cart']);
echo "SECOND";
}
else
{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['itemId']=$pid;
$_SESSION['cart'][0]['qty'] = $q;
$max=count($_SESSION['cart']);
}
}
函数addtocart($pid,$q)
{
如果($Pid不为第二个项目工作是什么意思?是覆盖第一个项目?还是不添加第二个项目?不为第二个项目工作是什么意思?是覆盖第一个项目?还是不添加第二个项目?产品id的冗余值作为kay以及a的子值有什么意义rray?没有技术上的原因;我想为将来的扩展准备空间。不知道用例,我不想偏离他在OP中建立的结构。这不会有什么坏处,如果在某些情况下,你只得到值,你不会得到一个没有上下文的平面数字数组。我如何回应pid
和qty
?我正在尝试这个$pid=$\u会话['cart'][$I]['itemId'];
和$q=$\u会话['cart'][$I]['qty'];
但是它们显示为空的,$I
是什么?foreach($u会话['cart']作为$item){print$item['itemId']:'.$item['qty'}
…然后,如果数量为0,则添加一些逻辑以完全删除数组项。这是您的项目负责人,使用它运行。我没有试图从每个角度思考,我只是回答了您的特定问题。如果($\u会话['cart'][$itemId]['qty']==0){unset($\u会话['cart'][$itemId])
产品id的冗余值作为kay以及阵列的子值有什么意义?没有技术原因;我假设为将来的扩展准备空间。不知道用例,我不想偏离他在OP中建立的结构。这不会造成伤害,而且如果有赢的情况d只有值,你不会在没有上下文的情况下得到一个简单的数字数组。我如何回显pid
和qty
?我正在尝试这个$pid=$\u会话['cart'][$I]['itemId'];
和$q=$\u会话['cart'][$I]['qty'];
但是它们显示为空…什么是$I
作为$item){print$item['itemId'].:'.$item['qty'];}
…如果数量为0,则添加一些逻辑以完全删除数组项。这是您的项目人员,请使用它运行。我不是试图从每个角度思考问题,我只是回答了您的特定问题。如果($\u SESSION['cart'][$itemId'['qty']==0){unset($\u SESSION cart['cart'][$itemId]);}