简体   繁体   English

如何使用ajax更新codeigniter购物车?

[英]How to update codeigniter shopping cart using ajax?

I want to update the quantity of a shopping cart when the item is already in the cart and add it when it is not(added in the cart) . 我想在购物车when the item is already in the cart 更新购物车的数量 ,而在购物车中when the item is already in the cart when it is not(added in the cart) 添加 when it is not(added in the cart) I'm confused on how to solve the twist of this problem. 我对如何解决这个问题感到困惑。 I've google a lot of the same problem but there is no same exact problem/solution for this. 我有很多相同的问题谷歌,但没有相同的确切问题/解决方案。

Here is the code of my view : 这是我认为的代码:

<?php if($lab): ?>
    <table class="table table-striped">
        <tr>
            <th>Description</th>
            <th>Price</th>
            <th>Action</th>
        </tr>
    <?php foreach($lab as $rows): ?>
        <tr>
            <td><?php echo $rows->ldesc; ?></td>
            <td><?php echo $rows->lprice; ?></td>
            <td><button value="<?php echo $rows->lid; ?>" class="btn btn-info addlabtype"><i class="icon-plus"></i></button></td>
        </tr>   
    <?php endforeach; ?>
    </table>
<?php endif; ?>
<script type="text/javascript" >
    (function(){
        $(document).on('click','.addlabtype',function(){
            var id= $(this).val(),
                dataString = "id=" + id;

                $.ajax({
                    type:"POST",
                    url: base_url + "inpatient/addlab",
                    data:dataString,
                    success:function(data){
                        $('.pickedlab').html(data);
                    }
                })
        });
    })()
</script>

Here is my controller : 这是我的控制器

public function addlab(){
    $data['cart'] = $this->inpatient_model->addlab();
    $this->load->view('admin/addeddiag',$data);     
}

And here is my model : 这是我的模型

  public function addlab(){
    $this->db->select('*');
    $this->db->from('lab');
    $this->db->where('lid',$this->input->post('id'));
    $q = $this->db->get();
    $lab = $q->result_array();
    $cart = $this->cart->contents();
    if($cart){
        foreach($cart as $items){
            if($this->input->post('id') == $items['id']  ){
                $this->cart->update(array(
                'rowid' => $items['rowid'],
                'qty' => $items['qty'] + 1
                ));                 
            }else{
                $data = array(
                    'id' => $lab[0]['lid'],
                    'name' => $lab[0]['ldesc'],
                    'qty' => 1,
                    'price' => $lab[0]['lprice']
                );
                $this->cart->insert($data);     
            }
        }
    }else{
        $data = array(
            'id' => $lab[0]['lid'],
            'name' => $lab[0]['ldesc'],
            'qty' => 1,
            'price' => $lab[0]['lprice']
        );
        $this->cart->insert($data);     
    }
    return $this->cart->contents();
  }

And yes one thing that is wrong in my code is the foreach in my model . 的,在我的代码中错误的一件事是模型中foreach I really have no idea on how to fix it. 我真的不知道如何解决它。 Because when I try to click .addlabtype for example I have 3 items in my cart and when I try to update it, it will not update unless it is the last item that I added in my cart. 因为例如当我尝试单击.addlabtype时,我的购物车中有3个商品,而当我尝试更新它时,除非它是我添加到购物车中的最后一个商品,否则它不会更新。 Sorry for my English. 对不起我的英语不好。 Thanks in advance! 提前致谢!

Edit : To sum it up, how can I get the rowid of a specific id in the cart? 编辑 :总结起来,我如何才能在购物车中获得特定idrowid

Finally problem solved I just change the value of my model with: 终于解决了问题,我只是使用以下方法更改了模型的值:

    $this->db->select('*');
    $this->db->from('lab');
    $this->db->where('lid',$this->input->post('id'));
    $q = $this->db->get();
    $lab = $q->result_array();
    $cart = $this->cart->contents();
    $found = false;
     foreach($cart as $items){

        if($this->input->post('id') == $items['id']  ){
                $this->cart->update(array(
                'rowid' => $items['rowid'],
                'qty' => $items['qty'] + 1
                ));     
                $found = true;
             }           
        }   
        if($found == false){
                $data = array(
                    'id' => $lab[0]['lid'],
                    'name' => $lab[0]['ldesc'],
                    'qty' => 1,
                    'price' => $lab[0]['lprice']
                );
        $this->cart->insert($data); 
    }

    return $this->cart->contents();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM