如何根据数据库更新购物车中所有商品的数量


How to update the quantity of all items in the cart with respect to database?

我正在使用代码点火器做购物车。虽然我可以做添加购物车功能,但我有点困惑如何更新它们与数据库有关。我想要的只是当我改变购物车中的商品数量并单击更新时,必须为该特定商品更新购物车和数据库。我试着去做,但做不到。有人能告诉我该怎么做吗?

控制器代码
 $this->load->model('user_m');
    $result['query'] = $this->user_m->get($id); // inserts coresponing item
    foreach ($result['query'] as $row) 
        $id = $row->pid;
        $qty = $a;
        $quan=$row->quantity;
        $price = $row->pprice;
    $name = $row->pname;
    $q=$quan-$a;     // for remainig stock i.e total qty-user qty
        $data = array(
        'id' => $id,
        'qty' => $qty,
        'price' => $price,
        'name' => $name,
        'stock' =>$q
    ); 
    $this->cart->insert($data);
    $this->load->model('user_m');
    $result['qry'] = $this->user_m->up_cart($id,$q);
   redirect($_SERVER['HTTP_REFERER']); 
}

请告诉我如何更新!

当您在购物车中添加任何产品时,它将生成唯一的行ID,并且每个产品都是唯一的,因此您需要根据行ID更新数量

       $data = array(
           'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
           'qty'   => 3
        );
      $this->cart->update($data);

//或多维数组

       $data = array(
           array(
                   'rowid'   => 'b99ccdf16028f015540f341130b6d8ec',
                   'qty'     => 3
                ),
           array(
                   'rowid'   => 'xw82g9q3r495893iajdh473990rikw23',
                   'qty'     => 4
                ),
           array(
                   'rowid'   => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
                   'qty'     => 2
                )
        );
      $this->cart->update($data); 

现在您想知道如何获得行id。

      $cart=$this->cart->contents();

它将返回整个购物车数组,当您在网页上列出它时,请将行id与数量文本框关联以更新该特定产品。

请查看以下网址:

https://ellislab.com/codeigniter/user-guide/libraries/cart.html