Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter中将用户id为的用户会话设置为购物车库_Php_Mysql_Ajax_Codeigniter - Fatal编程技术网

Php 在codeigniter中将用户id为的用户会话设置为购物车库

Php 在codeigniter中将用户id为的用户会话设置为购物车库,php,mysql,ajax,codeigniter,Php,Mysql,Ajax,Codeigniter,我正在使用codeigniter、ajax开发购物车 1) 登录用户后 2) 我正在向购物车提交项目列表(用于购买项目) 3) 我的问题是提交的数据现在显示给所有其他用户相同,我需要将用户id为的用户会话设置到codeigniter的购物车库中,以显示购物车项目以分离用户 users home.php: <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <div class="ro

我正在使用codeigniter、ajax开发购物车

1) 登录用户后

2) 我正在向购物车提交项目列表(用于购买项目)

3) 我的问题是提交的数据现在显示给所有其他用户相同,我需要将用户id为的用户会话设置到codeigniter的购物车库中,以显示购物车项目以分离用户

users home.php:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<div class="row">
    <div class="col-lg-12 text-center">
        <?php if(isset($_SESSION['loggedin'])){?>
            <div class="alert alert-success"><?php echo $_SESSION['loggedin'];?></div>
        <?php } ?>
        Hello, <?php echo $_SESSION['username']?>
    </div> 
</div>
<div class="row">
    <div class="col-lg-3">
        <table class="table table-condensed table-hover">
            <tr>
                <th class="text-center">Item</th>
                <th class="text-center">Quantity</th>
                <th class="text-center">Price</th>
                <th class="text-center">Add</th>
            </tr>
            <?php foreach($items as $item):?>
            <tr class="success">         
                <td><?php echo $item['product_name'];?></td>
                <td class="text-center"><input type="text" name="quantity" id="<?php echo $item['product_id'];?>" class="quantity" maxlength="2" size="2"></td>
                <td><?php echo $item['product_price'];?></td>
                <td><button type="button" name="add_cart" class="add_cart" data-productname="<?php echo $item['product_name'];?>" data-price="<?php echo $item['product_price'];?>" data-productid="<?php echo $item['product_id'];?>"><i class="fa fa-plus-circle"></i></button></td>
            </tr>
            <?php endforeach;?>
        </table>
    </div>
    <div class="col-lg-6 col-lg-offset-1">
        <div id="cart_details" class="text-center">
            <h3>Cart is Empty</h3>
        </div>
    </div>
</div>

<script>
$(document).ready(function(){
    $('.add_cart').click(function(){
    var product_id=$(this).data("productid");
    var product_name=$(this).data("productname");
    var product_price=$(this).data("price");
    var quantity=$('#' + product_id).val();
        if(quantity != '' && quantity >0){
            $.ajax({
                url:"<?php echo base_url();?>users/add",
                method:"POST",
                data:{product_id:product_id,product_name:product_name,product_price:product_price ,quantity :quantity},
                success:function(data)
                {
                alert("Product Added into cart");
                $('#cart_details').html(data);
                $('#' + product_id).val('');
                }
            });
        } else {
            alert("Please Enter Quantity");
        }
    });
    $('#cart_details').load("<?php echo base_url();?>users/load");

    $(document).on('click','.remove_inventory',function(){
        var row_id = $(this).attr("id");
        if(confirm("Are you sure you want to delete item")){
            $.ajax({
                url:"<?php echo base_url();?>users/remove",
                method:"POST",
                data:{row_id:row_id},
                success:function(data)
                {
                alert("Product remove fromm cart");
                $('#cart_details').html(data);
                }
            });
        }else{
            return false;
        }
    });
});

</script>

你好
项目
量
价格
添加

您的模型似乎没有根据发送请求的用户筛选项目,在我看来,在使用此代码时,您似乎从数据库中为每个用户的请求提取相同的项目

$this->db->get('tblItems');
对每个用户执行项目筛选,可以使用以下子句

$this->db->get_where('tblItems', array('user_id' => $userID));

基于用户的唯一ID和与tblItems表中某个位置的用户表的联接

,我们必须添加userid列?您必须找到将项目链接到用户的方法。添加一个用户列是一种方法,但更好的方法是创建一个单独的表,将用户ID和项目ID一起作为PK(规范化内容),然后在选择时,您可以使用表连接筛选出属于特定用户的项目。我们正在尝试将项目插入codeigniter的购物车库。我们不能在cart@peterasuming中存储userId,如果不能,如何将购物车链接到特定用户@anishkumarmagiri?或者,您如何知道哪个用户选择了哪些项目?所以,我们不应该使用购物车库,而是必须创建表到购物车。是的,彼得·M
$this->db->get('tblItems');
$this->db->get_where('tblItems', array('user_id' => $userID));