繁体   English   中英

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

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

我想在购物车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) 我对如何解决这个问题感到困惑。 我有很多相同的问题谷歌,但没有相同的确切问题/解决方案。

这是我认为的代码:

<?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>

这是我的控制器

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

这是我的模型

  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();
  }

的,在我的代码中错误的一件事是模型中foreach 我真的不知道如何解决它。 因为例如当我尝试单击.addlabtype时,我的购物车中有3个商品,而当我尝试更新它时,除非它是我添加到购物车中的最后一个商品,否则它不会更新。 对不起我的英语不好。 提前致谢!

编辑 :总结起来,我如何才能在购物车中获得特定idrowid

终于解决了问题,我只是使用以下方法更改了模型的值:

    $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