【问题标题】:Shopping cart in PHP not working accordinglyPHP中的购物车没有相应地工作
【发布时间】:2013-02-21 18:41:04
【问题描述】:

我一直在为购物车编写 PHP 代码而苦苦挣扎。

特别是添加商品的功能,可以很容易地拥有多个订单的商品。

这是我所拥有的,但它似乎不适用于添加第二个项目:

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']);
            }
    }

有什么建议吗?

谢谢

【问题讨论】:

  • 不为第二个项目工作是什么意思?它会覆盖第一项吗?还是只是不添加第二项?

标签: php session cart shopping


【解决方案1】:

你太复杂了。

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;
}

第一次使用此函数添加项目时,它将在会话数组中设置正确的条目。任何后续使用该函数来添加相同的项目都会增加qty 键。当一切都说完了,你会得到这样的东西:

array(
    '54'=>array(
        'itemId'=>54,
        'qty'=>4
    ),
    '99'=>array(
        'itemId'=>99,
        'qty'=>2
    ),  
)

文档

【讨论】:

  • 产品 id 的冗余值是 kay 以及数组的子值有什么意义?
  • 无技术原因;我假设为未来的扩张做准备。不知道用例,我不想偏离他在 OP 中建立的结构。这没有什么坏处,如果在某些情况下你只得到值,你就不会得到一个没有上下文的扁平数字数组。
  • 如何回显pidqty?我正在尝试这个 $pid=$_SESSION['cart'][$i]['itemId'];$q=$_SESSION['cart'][$i]['qty']; 但它们显示为空......
  • 什么是$iforeach ($_SESSION['cart'] as $item) { print $item['itemId'].': '.$item['qty']; }
  • ... 然后,如果数量为 0,则添加一些逻辑以完全删除数组条目。这是 您的 项目人员,使用它运行。我不是想从每个角度考虑,我只是回答了你的具体问题。 if ($_SESSION['cart'][$itemId]['qty'] == 0) { unset($_SESSION['cart'][$itemId]); }
【解决方案2】:

您的功能可以简化为这样的......

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;
    }

那以后再看……

foreach($_SESSION['cart'] as $item=>$qty){
    echo $item.': '.$qty;
}

这个方法很好,因为如果你添加两次相同的项目,数量会加在一起

【讨论】:

    【解决方案3】:

    也许这对你有帮助。第二个索引应该是$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']);
                }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2019-04-29
      • 2017-02-18
      • 2013-05-02
      相关资源
      最近更新 更多