views:

319

answers:

1

Hello,

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

+1  A: 

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.

RC
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.
Matt
I've updated my answer.
RC
Oh I see what you're saying - thanks for coming back to me. Let me try this out... I will report back
Matt