php ajax购物车系统只循环最后添加的产品

时间:2018-03-26 13:46:04

标签: php ajax cart

我有一个小脚本添加到购物车使用PHP和ajax,问题是,我不能添加多个产品,我的购物车下拉只显示我添加t cart的最后一个产品。我希望能够添加多个产品,它应该放在我的菜单上。

下面是我的代码

我的添加到购物车按钮

 <button type="button" class="btn btn-primary" onclick="add_cart('<?php echo $id; ?>')" ><i class="fa fa-shopping-cart"></i> Add To Cart</button>

javascript

<script>

function add_cart(p_id=""){
    var quantity = $(".quantity"+p_id).val();
    $.ajax({
        type:"post",
        url:"ajax_cart.php",
        data:{action:'add',p_id:p_id,quantity:quantity},
        success:function(result){
            $('.cart_data').html(result);
        }
    });
}


add_cart();


function remove_cart(p_id){
    //alert(p_id);
    $.ajax({
        type:"post",
        url:"ajax_cart.php",
        data:{action:'delete',p_id:p_id},
        success:function(result){
            $('.cart_data').html(result);
        }
    });
}

function empty_cart(){
    $.ajax({
        type:"post",
        url:"ajax_cart.php",
        data:{action:'empty'},
        success:function(result){
            $('.cart_data').html(result);
        }
    });
}

</script>

php

<?php session_start();
// include"conf.php";
$conn  = @mysql_connect("localhost","root","");
mysql_select_db("database",$conn);

$action = $_REQUEST['action'];
@$p_id   = trim($_REQUEST['p_id']);
$_SESSION['product_cart'] = array();
if($action == 'add'){
     @$quantity = $_REQUEST['quantity'];
     if(!empty($p_id)){
         $query = "select * from courses where id = '".$p_id."'";
         $rs = mysql_query($query,$conn) or die("failed".mysql_error());
         $product_data = mysql_fetch_assoc($rs);

         $product = array("p_id"=>$product_data['id'],"title"=>$product_data['name'],"price"=>$product_data['course_unit'],"image"=>$product_data['image'],"quantity"=>'10');

        if(isset($_SESSION['product_cart']) && !empty($_SESSION['product_cart']))
        {
            if(!array_key_exists($product_data['id'],$_SESSION['product_cart']))
            {

                $_SESSION['product_cart'][$product_data['id']] = $product;

            }
            else{

                $_SESSION['product_cart'][$product_data['id']]['course_unit'] = $_SESSION['product_cart'][$product_data['id']]['course_unit'] + ($product_data['course_unit']*$quantity);
                $_SESSION['product_cart'][$product_data['id']]['quantity'] = $_SESSION['product_cart'][$product_data['id']]['quantity']+$quantity;
            }       
        }
        else{
          $_SESSION['product_cart'][$product_data['id']] = $product;
        }
    }   
}
if($action == "delete"){
    unset($_SESSION['product_cart'][$p_id]);
}

if($action == "empty"){
    session_destroy();
}
?>

<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <img src="cart.png" style="width:40px" > <?php echo count(@$_SESSION['product_cart']); ?> - Items<span class="caret"></span></a>
          <ul class="dropdown-menu dropdown-cart" role="menu">
          <?php 
          if(isset($_SESSION['product_cart'])){
          foreach($_SESSION['product_cart'] as $data){
          ?>
              <li>
                  <span class="item">
                    <span class="item-left" style="color:#0000" >
                        <img src="upload/<?php echo $data['image']; ?>" alt="" style="width:60px;" />
                        <span class="item-info" style="color:#000;" >
                            <span><?php echo $data['title']; ?></span>
                            <span>Quantity : <?php echo $data['quantity']; ?></span>
                            <span>Price : <?php echo $data['price']; ?> INR</span>
                        </span>
                    </span>
                    <span class="item-right">
                        <button class="btn btn-xs btn-danger pull-right" onclick="remove_cart('<?php echo $data['p_id']; ?>')" >x</button>
                    </span>
                </span>
              </li>
          <?php }  } ?>
              <li class="divider"></li>
              <li><a class="text-center" href="">View Cart</a></li>
              <li><a class="text-center" href="#" onclick="empty_cart();" >Empty Cart</a></li>

?>

我猜它可能是会话的问题,即使我添加了多个项目,我也只在购物车中有一个项目。感谢

1 个答案:

答案 0 :(得分:0)

这是因为每次调用操作时都要初始化数组。您应该检查会话是否已经有值。

基本上你必须更换它:

$_SESSION['product_cart'] = array();

用这个:

$_SESSION['product_cart'] = ($_SESSION['product_cart'] ? $_SESSION['product_cart'] : array());