0

I am building a Codeigniter shopping cart. On the cart details page I have a form input field allowing the user to type in the quantity required of a product, and a submit button to post the information to the update function.

When there is just one item in the cart, when updating the quantity everything works as it should. However, when there is more than one item, changing the quantity of an item and clicking submit results in a ‘Undefined Offset 1: error on the following code in the Model (specifically the two lines within the array) :

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

This is the View code to which the above refers:

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

And this is the Controller:

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

Please can anyone explain why this is happening?

Many thanks,

Matt

3 Answers 3

2

instead of

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

use this

 for($i=0;$i < count($item);$i++) 
Sign up to request clarification or add additional context in comments.

Comments

2

I had the same issue; I'm pretty sure the issue is in the shopping cart view section. The hidden field isn't inside the foreach{} statement - that's why you can edit the quantity when you have one product in your shopping cart, but when you add another product can't edit the product. Here's a chunk of code that worked for me.

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

Comments

1

I believe that your problem is that you need to have

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

Namely, the 2 entries for the rowid and qty.

This link provides examples of using both standard and associative arrays with HTML inputs.

EDIT based on OP feedback:

This was the example I was referring too:

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

Another example:

<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 />"; }
}

Notice where the examples are using an input with the same "blah[]" for each value they expect to come back in the array. In your code, you have one rowid[] and one qty[] input in your view. For a single element this will work b/c you have one element defined in the array. when you have 2 items and you apparently are updating the total items variable to represent the correct number of items but then loop through trying to access the second element (i.e. 1) in each array which does not exist which is why you're getting "Undefined Offset 1" error.

2 Comments

Thanks RC, a useful article, though I don't see anything in it that would suggest the need for two of each input as you suggested. And doing this would result in two input fields for updating the quantity, which I wouldn't want.
Oh I see what you're saying - thanks for coming back to me. Let me try this out... I will report back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.