如何解决Codeigniter购物车更新问题

时间:2019-01-07 07:37:35

标签: codeigniter cart

我无法在结帐页面上更新购物车。我不知道发生了什么。 这是我的代码。

function update_cart()
{
  $rowid =$this->input->post('rowid');
  $price =$this->input->post('price');
  $qty = $this->input->post('qty');
  $pr = preg_replace( '/,/', '', $price);
  $amount = $pr * $qty;
  $data = array(
    'rowid' => $rowid,
    'price' => $price,
    'amount' => $amount,
    'qty' => $qty
  );
  $this->cart->update($data);
     redirect('checkout');
}

这是我的汽车查看代码。我正在此处尝试更新购物车值。没有错误显示。我无法解决。有帮助。只有更新购物车无法正常工作。其他功能运行良好。添加到购物车删除到购物车,但更新到购物车不起作用。我创建会话表,还尝试其他建议和解决方案,以防止堆栈溢出。

    <form action="<?=base_url()?>cart/update_cart" method="post">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <div class="panel-title">
                        <div class="row">
                            <div class="col-xs-6">
                                <h5><span class="glyphicon glyphicon-shopping-cart"></span> Shopping Cart</h5>
                            </div>
                            <div class="col-xs-6">
                                <a href="<?=base_url()?>">
                                <button type="button" class="btn btn-primary btn-sm btn-block">
                                    <span class="glyphicon glyphicon-share-alt"></span> Continue shopping
                                </button>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                <?php $i = 1; ?>
                <?php 
                //print_r($this->cart->contents() );
                //die;
                foreach ($this->cart->contents() as $items): ?>
                <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>

                    <div class="row">
                        <div class="col-xs-2">
                        <img class="img-responsive" src="<?=base_url()?>uploads/product/<?=$items['image'];?>" />
                        </div>
                        <div class="col-xs-4">
                            <h5 class="product-name">
                            <strong><?php echo $items['name']; ?>
                            </strong>
                            <?=$items['options'];?>
                            </h5>

                        </div>
                        <div class="col-xs-6">
                            <div class="col-xs-6 text-right">
                                <h6><strong><?= $this->cart->format_number($items['price']); ?>
                                <span class="text-muted">x</span></strong></h6>
                            </div>
                            <div class="col-xs-4">
                                <?php //echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'size' => '1')); ?>
                            <input  type="text" name="qty" min="1" value="<?=$items['qty'] ?>" maxlength="1" style="width: 50%;">
                            <input type="hidden" name="price" value="<?=$items['price'] ?>">
                            <input type="hidden" name="name" value="<?=$items['name'] ?>">
                            <input type="hidden" name="description" value="<?=$items['description'] ?>">
                            </div>
                            <div class="col-xs-2">
                                <a href="<?=base_url()?>cart/remove/<?=$items['rowid']?>">
                                <button type="button" class="btn btn-link btn-xs">
                                    <span class="glyphicon glyphicon-trash" onclick="confirm('Are you sure want to delete')";> </span>
                                </button>
                                </a>
                            </div>
                        </div>
                    </div>
                    <hr>


<?php $i++; ?>

<?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

在视图中....仅使用两个输入

   <input  type="text" name="qty[]" min="1" value="<?=$items['qty'] ?>" maxlength="1" style="width: 50%;">
<input type="hidden" name="rowid[]" value="<?=$items['rowid'] ?>">

在控制器上

如果您要更新购物车,则只需更改数量

    function update_cart()
{
for ($i=0; $i < count($_POST) - 1; $i++) { 
        $data = array(
            'rowid' => $_POST['rowid'][$i],
            'qty'   => $_POST['qty'][$i]
      );

    $this->cart->update($data);
      }
 redirect('checkout');
}