使用user_id将用户sesssion设置为codeigniter中的购物车库

时间:2017-10-11 17:35:04

标签: php mysql ajax codeigniter

我使用codeigniter,ajax开发购物车。

1)登录用户后

2)我提交物品清单(即购买物品)到购物车

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

用户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>

Controller.php这样

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

class Users extends CI_Controller 
{  
    public function index($page='home')
    {
        if(!file_exists(APPPATH.'views/users/'.$page.'.php')){
            show_404();
        }
        if(!$this->session->userdata('logged_in')){
            redirect('');
        }
        $data['title']=ucfirst($page);
        $data['items'] = $this->user_item->get_items();

        $this->load->view('templates/header');
        $this->load->view('users/'.$page,$data);
        $this->load->view('templates/footer');
    }


    public function add()
    {
        $this->load->library('cart');
        $data = array(
            'id' => $_POST["product_id"],
            'name' => $_POST["product_name"],
            'qty' => $_POST["quantity"],
            'price' => $_POST["product_price"],   
        );
        $this->cart->insert($data); //return rowid
        echo $this->view();
    }

    public function load()
    {
        echo $this->view();
    }

    public function remove()
    {
        $this->load->library('cart');
        $row_id = $_POST["row_id"];
        $data = array(
            'rowid' => $row_id,
            'qty' => 0
        );
        $this->cart->update($data); 
        echo $this->view();
    }

    public function view()
    {
        $this->load->library('cart');
        $output = '';
        $output.='
        <h3>Shopping cart</h3><br/>
        <div class="table-responsive">
            <div align="right">
            <button type="button" id="clear_cart" class="btn btn-danger"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
            </div>
            <br/>
        <table class="table table-bordered">
            <tr>
            <th class="text-center">Name</th>
            <th class="text-center">Quantity</th>
            <th class="text-center">Price</th>
            <th class="text-center">Total</th>
            <th class="text-center">Action</th>
            </tr>
        ';
        $count = 0;
        foreach($this->cart->contents() as $items){
            $count++;
            $output .='
            <tr>
                <td>'.$items["name"].'</td>
                <td>'.$items["qty"].'</td>
                <td>'.$items["price"].'</td>
                <td>'.$items["subtotal"].'</td>
                <td><button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="'.$items["rowid"].'"><i class="fa fa-times" aria-hidden="true"></i></button></td>
            </tr>
            ';
        }
        $output .='
            <tr>
                <td colspan="3" align="right">Total</td>
                <td>'.$this->cart->total().'</td>
            </tr>
        </table>
        <button type="button" name="add_to_cart" class="btn btn-danger btn-lg"><i class="fa fa-shopping-cart" aria-hidden="true"></i></button>
        </div>
        ';
        if($count == 0){
            $output = '<h3 align="center">cart is empty</h3>';
        }
        return $output;
    }
}

model.php

<?php
class User_item extends CI_Model
{
    public function __contruct()
    {
        $this->load-database();
    }

    public function get_items($ItemCode = FALSE)
    {
        if($ItemCode === FALSE)
            {
                $query = $this->db->get('tblItems');
                return $query->result_array();
            }
        $query = $this->db->get_where('tblItems',array('ItemCode' => $ItemCode));
        return $query->row_array();
    } 
}

1 个答案:

答案 0 :(得分:1)

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

$this->db->get('tblItems');

对每个用户执行项目过滤,可能使用

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

基于用户的唯一ID以及在某处使用用户表的连接