如何在这个购物车php中插入数量

时间:2017-12-16 04:43:46

标签: javascript php jquery html mysql

我已成功将商品添加到购物车。但是,我无法添加它的数量。我知道问题出在addtocart的某处,我无法将索引中的值传递给下一页。我仍然是这个的初学者,所以我不知道如何解决这个问题。我尝试过使用会话,但仍然无效

这是我在index.php中的代码:

<?php
    session_start();
    
if(isset($_SESSION['cart']) & !empty($_SESSION['cart'])){
	$items = $_SESSION['cart'];
	$cartitems = explode(",", $items);
	$items .= "," . $_GET['id'];
        if($_SERVER["REQUEST_METHOD"]=="POST"){
        $quantity = $_POST["quantity"];}
	header('location: index.php?status=success');
}else{
	$items = $_GET['id'];
	$_SESSION['cart'] = $items;
         if($_SERVER["REQUEST_METHOD"]=="POST"){
        $_POST["quantity"] = $quantity ;}
	header('location: index.php?status=success');
          
}
 ?>

这是我的addtocart.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            session_start();
            require_once('connection.php');
            include('header.php');
            include('nav.php');
            
            $items = $_SESSION['cart'];
            if($_SERVER["REQUEST_METHOD"]=="POST"){
                $quantity = $_POST["quantity"];
            }
            $cartitems = explode(",", $items);
            
       
        ?>
        
         
        <div class="container">
            <div class="row">
                <table class="table">
                    <tr>
                        <th>S.NO</th>
                        <th>Item Name</th>
                        <th>Quantity</th>
                        <th>Price </th>
                    </tr>
                    <?php
                        $total = 0;
                        $i =1;
                        
                        foreach ($cartitems as $key=>$id){
                            $sql = "SELECT * FROM products WHERE id = $id";
                            $res=  mysqli_query($connection, $sql);
                            if(empty($_SESSION['cart'])){
                                $r = 0;
                                
                            }
                            else{
                                 $r = mysqli_fetch_assoc($res);
      
                            }
                        ?>
                   
                    <tr>
                        <td><?php echo $i; ?></td>
                        <td><a href="delcart.php?remove=<?php echo $key; ?>">Remove</a><?php echo $r['title'];?></td>
                        <td><?php  if($_SERVER["REQUEST_METHOD"]=="POST"){echo $_POST["quantity"];};?></td>
                        <td>$<?php echo $r['price'];?></td>
                    </tr>
                    <?php
                    
                        $total = $total +  $r['price'];
                        $i++;
                        }
                    ?>
                    <tr>
                        <td><strong>Total Price</strong></td>
                        <td><strong>$<?php echo $total; ?></strong></td>
                        <td><a href="#" class="btn btn-info">Checkout</a></td>
                    </tr>
                </table>
            </div>
        </div>
        <?php 
            include 'footer.php';
        ?>
        
       
    </body>
</html>

这是我的cart.php:

changeLevel

1 个答案:

答案 0 :(得分:0)

查看我的购物车类,您可以根据需要进行修改

class cart {
/**
 * add product to cart by id and quantity
 * @param type $pid product id
 * @param type $quantity 
 */
function add($pid,$quantity){

/*if cart dosen't exist initialize it and add the first product*/
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart']=array();
        $_SESSION['cart'][0]['productid']=$pid;
        $_SESSION['cart'][0]['quantity']=$quantity;
        echo 'Product successfully added';
    }
/*else if cart exist check if product exist to change quantity else add new product*/
    else {
/*if exist change quantity*/
        if($this->isexist($pid,$quantity)){
            echo 'Quantity updated successfully';
        }
/*else add new product in cart*/
        else{
            $m=$_SESSION['cart'];
            $max=count($m);
            $_SESSION['cart'][$max]['productid']=$pid;
            $_SESSION['cart'][$max]['quantity']=$quantity;
            echo 'Product successfully added';
        }
    }

}

/**
 * check if product alredy exist in cart or not
 * @param type $pid product id
 * @param type $quantity
 * @return boolean if true the product is exist and the quantity change successfully
 */
function isexist($pid,$quantity) {
    $m=$_SESSION['cart'];
    $max=count($m);
    for($i=0;$i<$max;$i++){
        if($pid==$_SESSION['cart'][$i]['productid']){
            $_SESSION['cart'][$i]['quantity']=$quantity;
            return true;
        }
    }
    return false;

}

/**
 * delete product by product id
 * @param type $pid product id
 */
function delete($pid){
    $m=$_SESSION['cart'];
    $max=count($m);
    for($i=0;$i<$max;$i++){
        if($pid==$_SESSION['cart'][$i]['productid']){
        unset($_SESSION['cart'][$i]);
        $_SESSION['cart']=array_values($_SESSION['cart']);$_SESSION['cart'.'num']-=1;break;
        }
    }
}

/**
 * change quantity of exist product
 * @param type $pid product id
 * @param type $quantity
 */
function modify($pid,$quantity){
    $m=$_SESSION['cart'];
    $max=count($m);

    if($quantity>0){
        for($i=0;$i<$max;$i++){
            if($pid==$_SESSION['cart'][$i]['productid']){
                $_SESSION['cart'][$i]['quantity']=$quantity;break;
            }
        }
    }

    else {$this->delete($pid);}

}

/**
 * show all items in your cart
 */
function show_cart() {

    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        echo 'id=>'.$_SESSION['cart'][$i]['productid'].
             'quantity=>'.$_SESSION['cart'][$i]['quantity'].'<br>';
    }
}

}