如何在购物车功能codeigniter中更新其他值(数量,小计)

时间:2017-03-16 14:36:26

标签: php jquery codeigniter cart

我是codeigniter的初学者。我使用codeigniter的购物车库工作,它的工作正常。但我的列表购物车中有另外的值重量和金额(整数值)。例如,当我在两次添加相同的值时,只有数量和小计增加了。 重量和数量只是更新我最后一次输入不是用新值从旧值计算的。我怎么能解决这个问题? 我使用CI v3.1和数据表来显示列表购物车, 抱歉我的英语不好。最好的问候:)

这里是我的控制器购物车

//adding to cart list
public function addbarang()
{

    $data = array(
            'id' => $this->input->post('id_barang'),
            'price' => str_replace('.', '', $this->input->post('harga_barang')),
            'qty' => $this->input->post('qty'),
            'unit' => $this->input->post('j-unit'),
            'name' => $this->input->post('nama_barang'),
            'nama_jenis' => $this->input->post('j-nama_jenis'),
            'id_jenis' => $this->input->post('j-id_jenis'),
            'options' => array('j-berat' => $this->input->post('j-weight'),'j-amount' => $this->input->post('j-amount'))
        );
    $insert = $this->cart->insert($data);
    echo json_encode(array("status" => TRUE));
    //echo"<pre>";
    //print_r($this->cart->contents());
   // echo"<pre>";
}


//show cart list in datatables
public function ajax_list_transaksi()
{

    $data = array();

    $no = 1; 

    foreach ($this->cart->contents() as $items){

        $row = array();
        $row[] = $no;
        $row[] = $items["id"];
        $row[] = $items["name"];
        $row[] = $items["id_jenis"];
        $row[] = $items["nama_jenis"];
        $row[] = $items["qty"];
        $row[] = $items["unit"];
        $row[] = $items["options"]["j-berat"];
        $row[] = $items["options"]["j-amount"];
        $row[] = 'Rp. ' . number_format( $items['price'], 
                0 , '' , '.' ) . ',-';
        $row[] = 'Rp. ' . number_format( $items['subtotal'], 
                0 , '' , '.' ) . ',-';

        //add html for action
        $row[] = '<a href="javascript:void(0)" style="color:rgb(255,128,128); text-decoration:none" onclick="deletebarang('
                ."'".$items["rowid"]."'".','."'".$items['subtotal']."'".')"> <i class="fa fa-close"></i> Delete</a>';

        $data[] = $row;
        $no++;
    }

    $output = array(
                    "data" => $data,
            );
    echo json_encode($output);
}

我的js显示和添加价值购物车列表

//the js to show cart list in html

var table;
$(document).ready(function() {

  table = $('#table_transaksi').DataTable({ 
    "paging": true,
    "ordering": false,
    "info": false,
    "searching": false,
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": "<?= site_url('Master_rabp/ajax_list_transaksi')?>",
        "type": "POST"
    },
    "columnDefs": [{ 
      "targets": [ 0,1,2,3,4,5,6 ],
      "orderable": false,
    }],

  });
});

function reload_table()
{

  table.ajax.reload(null,false);

}

//the js for adding new cart value
function addbarang()
{

    var id_barang = $('#id_barang').val();
    var qty = $('#qty').val();
    if (id_barang == '') {
      $('#id_barang').focus();
    }else if(qty == ''){
      $('#qty').focus();
    }else{

      $.ajax({
        url : "<?= site_url('Master_rabp/addbarang')?>",
        type: "POST",
        data: $('#form_transaksi').serialize(),
        dataType: "JSON",
        success: function(data)
        {

           reload_table();
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error adding data');
        }
    });

      $('.reset').val('');
    };
}

function deletebarang(id,sub_total)
{
    // ajax delete data to database
      $.ajax({
        url : "<?= site_url('Master_rabp/deletebarang')?>/"+id,
        type: "POST",
        dataType: "JSON",
        success: function(data)
        {
           reload_table();
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error deleting data');
        }
    });
}

0 个答案:

没有答案