0

Codeigniter ショッピング カートを作成しています。カートの詳細ページには、ユーザーが製品に必要な数量を入力できるフォーム入力フィールドと、更新機能に情報を投稿するための送信ボタンがあります。

カートに商品が 1 つしかない場合、数量を更新するとすべて正常に機能します。ただし、複数のアイテムがある場合、アイテムの数量を変更して [送信] をクリックすると、モデル内の次のコード (具体的には配列内の 2 行) で「未定義のオフセット 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);  
        }
    }

これは、上記が参照する View コードです。

<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

それ以外の

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

これを使って

 for($i=0;$i < count($item);$i++) 
于 2011-09-21T21:00:53.087 に答える
2

同じ問題がありました。問題はショッピング カート ビュー セクションにあると確信しています。非表示フィールドは foreach{} ステートメント内にありません。そのため、ショッピング カートに商品が 1 つある場合は数量を編集できますが、別の商品を追加する場合は商品を編集できません。これが私のために働いたコードの塊です。

<?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 に答える
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[]」の入力を使用していることに注意してください。コードでは、ビューに1つのrowid[]と1つのqty[]入力があります。単一の要素の場合、これは、配列に1つの要素が定義されている場合に機能します。2つのアイテムがあり、アイテムの総数を更新して正しいアイテム数を表しているようですが、ループして、存在しない各配列の2番目の要素(つまり1 )にアクセスしようとしている場合は、次のようになります。未定義のオフセット1"エラー。

于 2010-05-10T12:01:12.097 に答える