如何使用 ajax 更新代码点火器购物车


How to update codeigniter shopping cart using ajax?

我想更新购物车when the item is already 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();