首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Codeigniter购物车更新数量错误

Codeigniter购物车更新数量错误
EN

Stack Overflow用户
提问于 2018-12-19 05:54:25
回答 1查看 0关注 0票数 0

我正在使用购物车类的codeigniter,当我在购物车上更新数量时,它不会更新正确的数字,而是它比我更新了更新。例如,如果我的总项目是5并且我将其更新为6,则显示超过6,可能是8或10或更多。

忽略计算中的点以及代码中的注释行。这只是数量更新错误

这是代码:

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

这是模型

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

}

这是控制器

function update_cart(){
    $this->Home_model->validate_update_cart();
    redirect($_SERVER['HTTP_REFERER']);
}
EN

回答 1

Stack Overflow用户

发布于 2018-12-19 15:31:22

首先,这个库已被弃用,它只是为了向后兼容而保留。

您获取错误数据的原因是您在视图中不使用row_id值,该值在更新时用于参考,请将您的输入与文档中的输入进行比较,我确信这可以很容易地修复:

<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>

<?php echo form_hidden('product_id', $items['id']); ?>

仔细看看,你还生成单个form_hidden值,而不是数组,在文档中有一个重要的界限:

 <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>

而您的代码生成以一个结果结束的输入 - product_id

<?php echo form_hidden('product_id', $items['id']); ?>

然后在更新方法中,您使用每个产品或一个产品的row_id和数量迭代$ this-> cart-> contents(),就可以了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档