PHP Codeigniter Undefined Offset Error

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-05-10T11:52:17Z Indexed on 2010/05/10 11:54 UTC
Read the original article Hit count: 510

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about php

Related posts about codeigniter