2

次の質問があります。

ショッピングカートをPHPセッションに保存し、それをロードしてカート内のアイテムを表示します。

問題は、現在、統合されていない各アイテムが表示されていることです。つまり、アイテムがカートに2回追加されると、2回表示されます。

itemIdで結果を統合するにはどうすればよいですか?

    if(is_array($_SESSION['cart']))
            {
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++)
{
    $pid=$_SESSION['cart'][$i]['itemId'];
    $q=$_SESSION['cart'][$i]['qty'];
    if($q==0) continue;
    $query2 = $con -> prepare("SELECT * FROM item_descr WHERE id_item = :idItem");
    $query2-> bindValue (':idItem',$pid);
    $query2->execute();
    $row2 = $query2->fetch(PDO::FETCH_ASSOC);
                    ?>
            <div class="checkoutItems">     
            <span class='itemNameChck'><?php echo $row2['name']; ?> </a></span>
            <span class="itemQtyChck"><?php echo $row2['price']; ?> </span>
            <span class="itemQtyChck"><?php echo $q; ?> </span>
            <span class="itemQtyChck"><?php $totalPerItem = $q * $row2['price']; echo $totalPerItem; ?> </span>
            </div>
            <?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'][$max]['qty']=$q;
                $max=count($_SESSION['cart']);
            }
            else
            {
                $_SESSION['cart']=array();
                $_SESSION['cart'][0]['itemId']=$pid;
                $_SESSION['cart'][0]['qty']=$q;
                $max=count($_SESSION['cart']);

            }
        //}
    }

ありがとう

4

2 に答える 2

3

私の意見では、カート項目のインデックスを含む ID を削除する必要があります。

//             I mean this
//                 ||
//                 ||
//                \  /
//                 \/
$_SESSION['cart'][$max]['qty']=$q;

そこに置くだけitemIdで、アイテムを追加する前に、配列のこの要素が設定されているかどうかを確認してください。

毎回配列に新しい要素を追加するこのコードを書きましたが、今度は統合したいと考えています。私には意味がありません。必要な場合にのみ新しい要素を追加します。それ以外の場合は、数量を更新してください。

私の意見では、カートは次のようになります。

$_SESSION['cart'][$itemid]['qty']=$q;

// example how to store other attributes
$_SESSION['cart'][$itemid]['name']="Rubber duck";
$_SESSION['cart'][$itemid]['color']="Yellow";
$_SESSION['cart'][$itemid]['size']="XXL";
$_SESSION['cart'][$itemid]['price']="1000";

次に、アイテムを追加するときに、そのitemidを持つカートアイテムに要素が含まれているかどうかを確認できます。

if isarray($_SESSION['cart'][$itemid]) // when exists - add to previous quantity
    $_SESSION['cart'][$itemid]['qty']= $_SESSION['cart'][$itemid]['qty'] + $q;
else                                   // else - set quantity
    $_SESSION['cart'][$itemid]['qty'] = $q;

これは単なる草案/アイデアですが、お役に立てば幸いです。

于 2013-02-19T21:59:33.333 に答える
0

Something like this should do the trick

function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;

for($i=0;$i> count($_SESSION['cart']);$i++)
{
    if($_SESSION['cart'][$i]["item_id"] == $pid)
    {   
        $_SESSION['cart'][$i]['qty'] += $q
        return;
    }
}


if(is_array($_SESSION['cart']))
{

    $max=count($_SESSION['cart']);
    $_SESSION['cart'][$max]['itemId']=$pid;
    $_SESSION['cart'][$max]['qty']=$q;
    $max=count($_SESSION['cart']);
}
else
{
    $_SESSION['cart']=array();
    $_SESSION['cart'][0]['itemId']=$pid;
    $_SESSION['cart'][0]['qty']=$q;
    $max=count($_SESSION['cart']);

}

}

于 2013-02-19T22:06:46.290 に答える