只能在购物车CODEIGNITER中添加一个项目


Can only add one item in cart CODEIGNITER

我似乎不能添加更多的项目,并且我添加的最新项目取代了现有的项目。

public function addcart(){
    if(isset($this->session->userdata)){
        $type = $this->session->userdata('type');
        $username = $this->session->userdata('username');
         $this->db->select('id_product ,price');
         $query = $this->db->get('product', array('title'=> $this->input->post('title')));
         if($query->num_rows() >0){
            $row = $query->row();
            $data = array(
            'id'     => $row->id_product,
           'qty'     => $this->input->post('quantity'),
           'price'   => $row->price,
           'name'    => $this->input->post('title'),
           //'options' => array('Size' => 'L', 'Color' => 'Red')
        );
        $this->cart->insert($data);
        }
    }
}
public function addcart(){
    if(isset($this->session->userdata)){
        $type = $this->session->userdata('type');
        $username = $this->session->userdata('username');
         $this->db->select('id_product ,price');
         $query = $this->db->get('product', array('title'=> $this->input->post('title')));
        $cart['product'] = $this->cart->contents(); // get previous data from cart
         if($query->num_rows() >0){
            $row = $query->row();
            $id = $row->id_product;
            $cart['product'][$id] = array(
            'id'     => $row->id_product,
           'qty'     => $this->input->post('quantity'),
           'price'   => $row->price,
           'name'    => $this->input->post('title'),
           //'options' => array('Size' => 'L', 'Color' => 'Red')
        );
        $this->cart->insert($cart); //update cart
        }
    }
}

controller:

$code = $this->input->post("idwheel");
$nama = $this->input->post("namawheel");
$base = $this->input->post("basewheel");
$desc = $this->input->post("descwheel");
$amountdefault = $this->input->post("amountwheel");
$data = array(
 'id'      => $code,
 'qty'     => 1,
 'amountdefault'     => $amountdefault,
 'price'   => $base,
 'name'    => $nama,
  'desc'    => $desc
    );
$this->cart->insert($data);
redirect("selling",'refresh');

和视图

<?php foreach ($this->cart->contents() as $items): ?>
<tr>
<td><?php echo $items['id']; ?></td>
<td><?php echo $items['name']; ?></td>
<td><?php echo $items['desc']; ?></td>
</tr>
<?php endforeach; ?>

希望对大家有帮助