1

私はショッピングカートのPHPコードに苦労しています。

特に、複数の注文数のアイテムを簡単に作成できるようにアイテムを追加する機能。

これが私が持っているものですが、2番目のアイテムを追加するためには機能しないようです:

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

助言がありますか?

ありがとう

4

3 に答える 3

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
    ),  
)

ドキュメンテーション

于 2013-02-21T18:48:40.690 に答える
0

あなたの関数は、このようなものに単純化することができます..

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

同じアイテムを2回追加すると、数量が一緒に追加されるため、この方法は良いです

于 2013-02-21T18:47:45.837 に答える
0

多分これはあなたを助けます。2 番目のインデックスは$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']);
            }
    }
于 2013-02-21T18:48:41.453 に答える