CodeIgniter:我无法从购物车中删除该商品

时间:2012-08-16 13:22:55

标签: php codeigniter

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

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');
}

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

2 个答案:

答案 0 :(得分:2)

使用此功能从购物车会话中删除商品:

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');
}

答案 1 :(得分:0)

最好使用:

  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');

我们直接从模型重定向。可能它有时会出现问题,但大多数情况下它会失败...... 我希望它有用....

   }