dqwr32867 2018-12-18 04:25
浏览 28

Codeigniter购物车更新数量错误

I am using shopping cart class of codeigniter and when I update qty on cart, it doesn't update correct number instead it got update more than i did. for example, if my total item is 5 and I update it to 6, it show more than 6, may be 8 or 10 or more.

Ignore the point calculated as well as commented lines in the code guys. it is just the quantity update error

Here is the code:

<tbody>
<?php $i = 1;
$point = 0;
?>
<?php foreach ($this->cart->contents() as $items):
    $point += $items['qty'] * $items['options']['item_point'];
    ?>
    <?php echo form_hidden('product_id', $items['id']); ?>
    <tr <?php if ($i & 1) {
        echo 'class="alt"';
    } ?>>
        <td class="col-sm-8 col-md-6">
            <div class="media">
                <a class="thumbnail pull-left" href="#">
                    <img class="media-object" src="<?php echo $items['img']; ?>"
                         style="width: 72px; height: 72px;padding-left: 2px;"> </a>
                <div class="media-body" style="padding-left: 13px;padding-top: 9px;">
                    <h4 class="media-heading"><a href="#"><?php echo $items['options']['product_name']; ?></a></h4>
                </div>
            </div>
        </td>
        <td class="col-sm-1 col-md-1" style="text-align: center">
            <input type="number" name="quantity" min="0" max="10" value="<?php echo $items['qty'] ?>" size='5'
                   maxlength='3' class="form-control">
        </td>
        <td class="col-sm-1 col-md-1 text-center">
            <strong><?php echo $items['qty'] * $items['options']['item_point']; ?></strong></td>
        <td class="col-sm-1 col-md-1 text-center"><strong><?php echo $this->cart->format_number($items['price']); ?>
                Ks</strong></td>
        <td class="col-sm-1 col-md-1 text-center"><strong><?php echo $this->cart->format_number($items['subtotal']); ?>
                Ks</strong></td>

    </tr>
    <?php $i++; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
    <td><h4>Total</h4></td>
    <td></td>
    <td class="text-center"><h3><?php echo $point; ?></h3></td>
    <!-- <td><h5>Subtotal<br>Estimated shipping</h5><h3>Total</h3></td>
    <td class="text-right"><h5><strong>$24.59<br>$6.94</strong></h5><h3>$31.53</h3></td> -->
    <td></td>
    <td class="text-right"><h3><?php echo $this->cart->format_number($this->cart->total()); ?> Ks</h3></td>
</tr>

<tr>
    <td></td>
    <td></td>
    <td>
        <div class="btn btn-default">
            <span class="glyphicon glyphicon-shopping-cart"></span> <?php echo anchor('front/home/empty_cart', 'Empty Cart', 'class="empty"'); ?>
        </div>
    </td>
    <td>
        <button type="submit" class="btn btn-primary">
            <span class="glyphicon glyphicon-shopping-cart"></span> Update your Cart
        </button>
         
    </td>
    <td>
        <div class="btn btn-success">
            <a href="<?php echo base_url(); ?>front/payment" title="Checkout" style="color: #FFFFFF">Checkout</a> &nbsp;
            <span class="glyphicon glyphicon-play"></span>
        </div>
    </td>
</tr>
</tfoot>
</table>
<?php
echo form_close();

Here is the model

function validate_update_cart()
{

    // Get the total number of items in cart
    $total = $this->cart->total_items();

    // Retrieve s posted information
    $item = $this->input->post('item_id');
    $qty = $this->input->post('quantity');

    // Cycle true all items and update them
    for ($i = 0; $i < $total; $i++) {
        //Create an array with the products rowid's and quantities.
        $data = array(
            'id' => $item[$i],
            'qty' => $qty[$i]
        );

        //Update the cart with the new information
        $this->cart->update($data);
    }
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    exit;
    // $data = array('item_id'=>$item,'qty'=>$qty);
    // $this->cart->update($data);

}

Here is the controller

function update_cart(){
    $this->Home_model->validate_update_cart();
    redirect($_SERVER['HTTP_REFERER']);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了
    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能