Question

I'm currently learning cart system with CI and got some problems

A PHP Error was encountered 
Severity: Notice
Message: Undefined offset: 2
Filename: models/main_model.php
Line Number: 241

here's the code:

Controller:

function update_cart(){
    $this->main_model->validate_update_cart();
    redirect('cart');
}

Model:

function validate_update_cart(){

    // Get the total number of items in cart
    $total = $this->cart->total_items();

    // Retrieve the posted information
    $item = $this->input->post('rowid');
    $qty = $this->input->post('qty');

    // Cycle true all items and update them
    for($i=0;$i < $total;$i++)
    {
        // Create an array with the products rowid's and quantities. 
        $data = array(
           'rowid' => $item[$i], //this is line 241
           'qty'   => $qty[$i]
        );

        // Update the cart with the new information
        $this->cart->update($data);
    }

}

view:

<div id="main">
<?php if(!$this->cart->contents()):
    echo 'You don\'t have any items yet.';
else:
?>

<?php echo form_open('cart/update_cart'); ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <td style="background-color:yellow;">Qty</td>
            <td style="background-color:yellow;">Item No/td>
            <td style="background-color:yellow;">Description</td>
            <td style="background-color:yellow;">Color</td>
            <td style="background-color:yellow;">Price</td>
            <td style="background-color:yellow;">Sub-Total</td>
            <td style="background-color:yellow;">Delete</td>

        </tr>
    </thead>
    <tbody>
        <?php $i = 1; ?>
        <?php foreach($this->cart->contents() as $items): ?>

        <?php echo form_hidden('rowid[]', $items['rowid']); ?>
        <tr <?php if($i&1){ echo 'class="alt"'; }?>>
            <td>
                <?php echo form_input(array('name' => 'qty[]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>
            </td>
            <td><a style="font-size:11px;"><?php echo $items['id']?></a></td>
            <td><a style="font-size:11px;"><?php echo $items['name']; ?></a></td>
            <td><a style="font-size:11px;"><?php echo $items['warna']?></a></td>
            <td><a style="font-size:11px;">Rp. <?php echo number_format($items['price'],0,",",".");?></a></td>
            <td><a style="font-size:11px;">Rp. <?php echo number_format($items['subtotal'],0,",",".");?></a></td>
            <td><a href="<?= base_url();?>cart/delete/<?= $items['rowid'];?>"><img src="<?= base_url();?>assets/image/hapus.png"></img></a></td>
        </tr>

        <?php $i++; ?>
        <?php endforeach; ?>
        <tr>
            <td colspan="5"><strong>Total</strong></td>
            <td colspan="2"><a align="right"><?php echo $this->cart->format_number($this->cart->total()); ?></a></td>
        </tr>
    </tbody>
</table>

<p><?php echo "<input type='submit' class='Button' value='Update'>";?></p>
<?php 
echo form_close(); 
endif;
?>

edited the view, complete code. when i click the update button it returned error live above. thanks.

Was it helpful?

Solution

It seems that $this->cart->total_items() returns something different from the total items in the $item and $qty arrays. Later on, you are using this result to iterate these arrays, and the loop variable ($i) exceeds the arrays' boundaries.

Change your loop to:

for($i=0;$i < count($item);$i++)

You can choose count($qty) if you prefer, provided that the two arrays contain the same number of elements (which has to be true anyway, in order for the whole algorithm to work).

OTHER TIPS

when you receive the post you should use

rowid[] instead of rowid

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top