0

我正在构建一个 Codeigniter 购物车。在购物车详细信息页面上,我有一个表单输入字段,允许用户输入产品所需的数量,以及一个提交按钮,用于将信息发布到更新功能。

当购物车中只有一件商品时,更新数量时,一切正常。但是,当有多个项目时,更改项目的数量并单击提交会导致模型中以下代码(特别是数组中的两行)出现 'Undefined Offset 1: 错误:

function validate_update_cart()
    { 
        $total = $this->cart->total_items();  

        $item = $this->input->post('rowid');  
        $qty = $this->input->post('qty');  

        for($i=0;$i < $total;$i++)  
        {  

            $data = array(  
                  'rowid' => $item[$i], 
                  'qty'   => $qty[$i]  
               );  

            $this->cart->update($data);  
        }
    }

这是上面提到的视图代码:

<form action="<?php echo base_url(); ?>home/update" method="post">
        <div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div>
        <div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div>
        <div><input type="submit" value="update" class="update-quantity"/></div>
</form>

这是控制器:

function update()
    {
    $this->products_model->validate_update_cart();  
        redirect('cart');
    }

请任何人解释为什么会这样?

非常感谢,

马特

4

3 回答 3

2

我遇到过同样的问题; 我很确定问题出在购物车视图部分。隐藏字段不在 foreach{} 语句中 - 这就是为什么您可以在购物车中有一个产品时编辑数量,但当您添加另一个产品时无法编辑该产品。这是一段对我有用的代码。

<?php if ($this->cart->total_items()!=0) :?>
<div id="cart">

  <?php  if ($cart=$this->cart->contents()) :?>
  <table>
    <caption>Shopping Cart</caption>
    <thead>
      <th>Item Name</th>
      <th>Option</th>
      <th>Price</th>
      <th>Qty</th>
      <th></th>
    </thead>
    <?php foreach ($cart as $item): ?>
    <tr>
      <td><?=$item['name'];?></td>
      <td>
        <?php
           if ($this->cart->has_options($item['rowid'])) {
               foreach ($this->cart->product_options($item['rowid']) as $option => $value) {
                   echo $option.": <em> ".$value." </em>";
                };
            };
        ?>
      </td>
      <td>$<?=$item['subtotal'];?></td>
      <?=form_open('index.php/shop/update_cart'); ?>
      <td>
        <?=form_input(array('name' => 'qty[]', 'value' => $item['qty'], 'maxlength' => '2', 'size' => '2')); ?>
      </td>
      <td class="remove"><?=anchor('index.php/shop/delete/'.$item['rowid'],'X','class="remove"');?></td>
      <td> <?=form_hidden('rowid[]', $item['rowid']); ?></td>
    </tr>
    <?php endforeach;?>
    <tr class="total">
      <td colspan="2"> <strong>Total</strong> </td>
      <td>$<?=$this->cart->total();?></td>
    </tr>
    <tr>
      <td><?php echo form_submit('submit', 'Update your Cart'); ?></td>
      <!-- When you want to empty your cart using ajax, add 'class="empty"' as a third parameter. -->
      <td><?=anchor('index.php/shop/empty_cart', 'Empty Cart', 'class="empty"');?></td>
      <?=form_close();?>
    </tr>
  </table>
  <?php endif;?>
</div>
<?php endif;?>
于 2012-07-27T03:58:46.327 回答
2

代替

 for($i=0;$i < $total;$i++) 

用这个

 for($i=0;$i < count($item);$i++) 
于 2011-09-21T21:00:53.087 回答
1

我相信你的问题是你需要有

<form action="<?php echo base_url(); ?>home/update" method="post">
        <div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div>
        <div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div>

        <div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div>
        <div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div>

        <div><input type="submit" value="update" class="update-quantity"/></div>
</form>

即,rowid 和 qty 的 2 个条目。

链接提供了将标准数组和关联数组与 HTML 输入一起使用的示例。

根据 OP 反馈进行编辑:

这也是我所指的例子:

<label><input type="checkbox" name="choice[]" value="1"/> 1</label>
<label><input type="checkbox" name="choice[]" value="2"/> 2</label>
<!-- etc... -->

// meanwhile, on the server...
$choice = $this->input->post('choice');
print_r($choice); // Array ( [0] => 1 [1] => 2 );

另一个例子:

<form method="post" action="">
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input type="submit" value="Submit" />
</form>

// *****  Server-Side PHP: *****

// Loop through the friend array
foreach ($_POST['friend'] as $value) {
    if ($value) { echo $value."<br />"; }
}

请注意,示例在哪里使用具有相同“blah []”的输入来表示他们希望返回到数组中的每个值。在您的代码中,您的视图中有一个 rowid[] 和一个 qty[] 输入。对于单个元素,这将起作用 b/c 您在数组中定义了一个元素。当您有 2 个项目并且您显然正在更新 total items 变量以表示正确的项目数但然后循环尝试访问每个数组中不存在的第二个元素(即1 ),这就是您得到“未定义的偏移量 1" 错误。

于 2010-05-10T12:01:12.097 回答