【问题标题】:Strange CI3 Cart issue on update更新时出现奇怪的 CI3 购物车问题
【发布时间】:2015-07-15 20:42:32
【问题描述】:

使用 Codeigniter 3 和本机购物车库。当我更改数量并更新购物车时,如果我从 1 更改为 2,它工作正常。如果我更改为 3 或更高的数字,我会收到错误,尽管它仍然会将数量更新为 3。如果我再更改回 1,我会收到错误,一旦回到 1,我再次更改为 2 就可以了。

错误:

Severity: Notice

Message: Undefined offset: 1

Filename: models/Cart_model.php

Line Number: 60

第60行是

 'rowid' => $item[$i]

购物车:

<?php echo form_open('cart/update'); ?>  
                  <table class="table">
                    <thead>
                      <tr>
                        <th class="text-center"><i class="fa fa-shopping-cart"></i>
                        </th>
                        <th>Product Name</th>
                        <th>Quantity</th>
                        <th>Total</th>
                        <th></th>
                      </tr>
                    </thead>
                    <tbody>
                        <?php $i = 1; ?>

                        <?php foreach ($this->cart->contents() as $items): ?>
                        <?php echo form_hidden('rowid[]', $items['rowid']); // We added an hidden field which contains a unique id in array format, this is needed in order to update ?>

                      <tr>
                        <td class="table-first">

                          <img class="img-responsive" src="<?= site_url('assets/images/shop/CreamseedBoilies.jpg') ?>" alt=""/>
                        </td>
                        <td>
                          <span class="title"><?php echo $items['name']; ?></span>
                        </td>
                        <td>
                          <?php echo form_input(array('name' => 'qty[]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>
                        </td>
                        <td>
                          <span><?php echo $this->cart->format_number($items['price']); ?></span>
                        </td>
                        <td class="close-cart">
                          <a href="<?= site_url('cart/remove/'.$items['rowid']) ?>" class="btn btn-primary btn-sm"><i class="fa fa-times"></i>
                          </a>
                        </td>
                      </tr>
                      <?php $i++; ?>

                        <?php endforeach; ?>

                    </tbody>
                  </table>

                  <div class="panel-footer">
                    <div class="row">
                      <div class="col-sm-3">
                        <a href="<?= site_url('cart/empty') ?>" class="btn btn-default btn-sm">Empty Cart</a>
                      </div>
                      <div class="col-sm-9 text-right">
                        <button type="submit" class="btn btn-default btn-sm">Update Cart</button>
                        <a href="#" class="btn btn-primary btn-sm">Proceed to checkout</a>
                      </div>
                    </div>
                  </div>
                  <?php echo form_close() ?>

购物车更新方法:

        public function update()
{

        $this->cart_model->validate_update_cart();
        redirect('cart');


}

推车型号:

function validate_update_cart(){

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

// Retrieve the posted information
$item = $this->input->post('rowid');
$qty = $this->input->post('qty');
// 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(
       'rowid' => $item[$i],
       'qty'   => $qty[$i]
    );

    // Update the cart with the new information
    $this->cart->update($data);
}

}

【问题讨论】:

  • 尝试搜索错误信息。您将了解为什么会发生Undefined offset 错误。从 $item 数组和 $total 的代码大小来看是不一样的。
  • 试图访问未设置的数组索引?但它已设置,如果我 print_r($item) 我可以看到它已设置,所以这没有帮助
  • 我想你没看懂我的评论。size of $item array and $total is not same该项目只有一个索引(大小=1)。但是您尝试访问不存在第二个索引的第一个和第二个索引。
  • 为什么要使用循环更新?
  • cart->total_items() 没有给出唯一 rowid 的计数,所以如果我在购物车中有 8 个产品,它会显示 8 个项目,但是 rowid 只有一个。谢谢你。我不知道您为什么没有将其作为答案提交。我使用 $total= count($item);它已经解决了。仍然不知道如何搜索错误消息对我有帮助。

标签: php codeigniter shopping-cart


【解决方案1】:

事实证明:

$total = $this->cart->total_items();

不提供唯一产品的总数(rowid),而是提供包括多个产品在内的项目总数。因此 $total 和 total items 的大小不匹配。

正确方法:

function validate_update_cart(){

// Get the total number of items in cart

// Retrieve the posted information
$item = $this->input->post('rowid');
$qty = $this->input->post('qty');

$total = count($item);
// 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(
       'rowid' => $item[$i],
       'qty'   => $qty[$i]
    );

    // Update the cart with the new information
    $this->cart->update($data);
}

}

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 2014-08-29
    相关资源
    最近更新 更多