购物车问题

时间:2011-09-19 17:21:23

标签: php cart shopping

我有这个购物车

function modifyCart($action, $id){

$kori = cart();
$exp = explode("|", $kori);

for($i=0;$i<count($exp);$i++){
$pilk = explode("-", $exp[$i]);
if($id == $pilk[0]){
    switch($action){
        case "increase": // lisätään määrää
        break;

        case "decrease": // vähennetään määrää
        break;

        case "remove": // poistetaan
        unset($exp[$i]);
    }
 }
}

$valmis = array_merge($exp, $pilk);

$muuta  = implode("|", $valmis);

$_SESSION["cart"] = $muuta;


}

购物车的存储方式与productno-qty|productno-qty相同 我怎么能让这个功能起作用..感谢帮助

1 个答案:

答案 0 :(得分:1)

这是一个糟糕的推车。但如果你被困在这条赛道上,你想要这个:

function modifyCart($action, $id){

$kori = cart();
$exp = explode("|", $kori);

foreach ($key, $product in $exp) {
    $tmp = explode('-', $product);
    $productNo = $tmp[0];
    $productQty = $tmp[1];

    if ($id == $productNo) {
        if ($action == 'remove') {
            unset($exp[$key]);
            return implode('|', $exp);
        } else if ($action == 'increase') {
            $productQty += 1;
            $exp[$key] = $productNo . '-' $productQty;
            return implode('|', $exp);
        } else if ($action == 'decrease') {
            $productQty -= 1;
            if ($productQty == 0) {
                unset($exp[$key]);
            } else {
                $exp[$key] = $productNo . '-' $productQty;
            }
            return implode('|', $exp);
        } else {
            // throw exception because unrecognized action
        }
    }
    // handle case where named product was not in the cart here.
    return implode('|', $exp);
}