代码点火器:我无法从购物车中删除商品


CodeIgniter: I can't remove the item from the cart

我正在尝试通过在控制器中发出命令来从购物车中删除该项目。这是我的代码:

function remove($rowid) {   
    $data = array(
        'rowid'   => $rowid,
        'qty'     => 0
    );
    $this->cart->update($data);
    redirect('bookings');
}

现在,当我单击链接以删除该项目时,它会返回错误"找不到 404 页面"。这是示例网址:

http://example.com/reservation/bookings/remove/c81e728d9d4c2f636f067f89cc14862c

"remove()"

函数与我的"add()"位于同一文件中,可以正常工作。

这是我的"add()"函数的代码:

public function add()
{
    $this->load->model('Bookings_model');
    $this->load->helper('url');
    $this->load->library('session');
    $this->load->library('cart');
    $bookings = $this->Bookings_model->get($this->input->post('id'));
    $data['type'] = $this->input->post('type');
    $data['checkin'] = $this->input->post('checkin');
    $data['checkout'] = $this->input->post('checkout');
    $data['nights'] = (strtotime($data['checkout']) - strtotime($data['checkin'])) / (60 * 60 * 24);
    $insert = array(
        'id' => $this->input->post('id'),
        'name' => $bookings->room_type,
        'checkin' => $data['checkin'],
        'checkout' => $data['checkout'],
        'nights' => $data['nights'],
        'price' => $bookings->default_price,
        'qty' => 1,
        'amount' => $bookings->default_price
    );
    $this->cart->insert($insert);
    redirect('bookings');
}

尝试了一切,但现在两天了,我仍然找不到解决方案。

使用此

函数从购物车会话中删除商品:

function remove($rowid){
    $this->load->library('cart');
    $data = array();
   foreach($this->cart->contents() as $items){
       if($items['rowid'] != $rowid){
           $data[] = array('id' => $items['rowid'],
                           'qty' => $items['qty'],
                           'price' => $items['price'],
                           'name' => $items['name']);
       }
   }
   $this->cart->destroy();
   $this->cart->insert($data);
   redirect('bookings');
}

最好使用:

  public function delete_item($id)
    {            
        $this->db->where('itemid', $id);
        $res = $this->db->delete('mycart');
        return $res; 
    } 

在控制器中:

   function remove()
   {
         $result = $this->model_name->delete_item($id);
         if($result)
                    redirect('view or controller');

我们直接从模型重定向,可能会在某个时候扭动,但大多数情况下它会失败......我希望它有用。

   }