簡體   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