产品未添加到购物车

时间:2014-02-12 02:40:53

标签: php html

我的购物车脚本出现问题,我已经设法通过向购物车添加商品来使其工作,但它不允许添加任何更多的商品,但它允许添加数量,即使我添加另一个项目,它将增加的第一个项目的数量,这是我的代码片段

这是我的函数脚本,用于编写并生成购物车

 <?php

function writeShoppingCart() {
    $cart = $_SESSION['cart'];
    if (!$cart) {
        return '<p>You have no items in your shopping cart</p>';
    } else {
        // Parse the cart session variable
        $items = explode(',',$cart);
        $s = (count($items) > 1) ? 's':'';
        return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
    }
}

function showCart() {
    $currency = 'UGX';
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
        $output[] = '<table class="table table-striped">';
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM products WHERE product_id = '.$id;
            $result = $db->query($sql);
            $row = $result->fetch();
            extract($row);
            $output[] = '<tr>';
            $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
            $output[] = '<td>'.$product_name.'</td>';
            $output[] = '<td>'.$currency.''.$product_price.'</td>';
            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
            $output[] = '<td>'.$currency.''.($product_price  * $qty).'</td>';
            $total += $product_price * $qty;
            $output[] = '</tr>';
        }
        $output[] = '</table>';
        $output[] = '<p>Grand total: <strong>'.$currency.''.$total.'</strong></p>';
        $output[] = '<div><button type="submit">Update cart</button></div>';
        $output[] = '</form>';
    } else {
        $output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
}

?>

这是我的购物车脚本,用于处理购物车按钮(添加,删除和更新)

中的操作
<?php
    session_start();
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
    case 'add':
        if ($cart) {
            $cart .= ','.!isset($_GET['product_id']);
        } else {
            $cart = !isset($_GET['product_id']);
        }
        break;
    case 'delete':
        if ($cart) {
            $items = explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) {
                if (!isset($_GET['product_id']) != $item) {
                    if ($newcart != '') {
                        $newcart .= ','.$item;
                    } else {
                        $newcart = $item;
                    }
                }
            }
            $cart = $newcart;
        }
        break;
    case 'update':
    if ($cart) {
        $newcart = '';
        foreach ($_POST as $key=>$value) {
            if (stristr($key,'qty')) {
                $id = str_replace('qty','',$key);
                $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                $newcart = '';
                foreach ($items as $item) {
                    if ($id != $item) {
                        if ($newcart != '') {
                            $newcart .= ','.$item;
                        } else {
                            $newcart = $item;
                        }
                    }
                }
                for ($i=1;$i<=$value;$i++) {
                    if ($newcart != '') {
                        $newcart .= ','.$id;
                    } else {
                        $newcart = $id;
                    }
                }
            }
        }
    }
    $cart = $newcart;
    break;
}
$_SESSION['cart'] = $cart;
?>

这就是从添加按钮

向我的购物车发送动作的方法
<form action="cart.php?action=add&id=<?php echo $row_prdt_details['product_id']; ?>" method="post" id="cart">
                        <fieldset>
                        <address>
                                <strong>Name:</strong> <span><?php echo $row_prdt_details['product_name']; ?></span><br>
                                <strong>Cake Code:</strong> <span><?php echo $row_prdt_details['product_id']; ?></span><br>
                                <strong>Availability:</strong> <span>
                                                                    </span><br>                             
                            </address>                                  
                            <h4><strong>Price: UGX <?php echo $row_prdt_details['product_price']; ?></strong></h4>              
                                <p>&nbsp;</p>
                                <label>Qty:</label>
                                <input type="text" class="span1" name="qty" placeholder="1">
                                <input type="submit" onClick="window.location.reload()" />
                                </fieldset>
                            </form>

我想要实现的是能够添加更多项目 非常欢迎任何帮助,如果您对正在做什么有任何疑问请咨询我

0 个答案:

没有答案