Update carrinho codeigniter

Asked

Viewed 321 times

1

I’m having a problem updating the quantity in a shopping cart using codeigniter, because whenever I update and the cart has for example 3 items, the cart only updates the last item and does not update the first 2.

View code

<?php echo form_open('shop/update_car'); ?>


    <?php foreach ($this->cart->contents() as $item): ?>
        <tr>
            <td><?php echo $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><?php echo form_input(array('name' => 'qtde[]', 'value' => $item['qty'], 'size' => '2')); ?></td>
             <?php echo form_hidden('rowid[]', $item['rowid']); ?>
            <td>R$ <?php echo number_format($item['price'],2,',','.'); ?></td>
            <td>R$ <?php echo number_format($item['subtotal'],2,',','.'); ?></td>
            <td class="remove">
                <?php echo anchor('shop/remove/'.$item['rowid'],'X'); ?>
            </td>
        </tr>
    <?php endforeach; ?>
    <tr class="total">
        <td colspan="3"></td>
        <td><?php echo form_submit('', 'atualizar carrinho');?></td>

        <td><strong>Total</strong> R$ <?php echo number_format($this->cart->total(),2,',','.'); ?></td>
    </tr>
    </table>        
</div>

code controller shop

function update_car(){

    $value    = $this->input->post('rowid');
    $quantity = $this->input->post('qtde');

    foreach($value as $values){

        foreach($quantity as $quantitys){

            $update = array(

            'rowid' => $values,
            'qty' => $quantitys 

            );      

        }

    }



    $this->cart->update($update);

    redirect('shop');       
} 
  • the update() is only applied in the latter, because it is out of the foreach and also by the fact of $update be reallocated every time around the foreach.

  • hi, I already put inside the inner foreach, and msm so it didn’t work, now ta if I update the last one it is updating all!!

  • A normal person seems better in this situation I imagine rowid and qtde be array with the same number of elements, then just hit the Dice and send to the bank, this kills the most internal foreach.

  • Can you pass me how I can implement this, please

  • Someone else can help me!

1 answer

0


Problem:

Like the update() is out of foreach only the last item will be updated see that the assignment to $update is remade every lap of the second foreach.

Solution

It seems that $value and $quantity has the same number of elements and are related, in which case it is better to exchange the foreachs for a normal is.

function update_car(){
    $value    = $this->input->post('rowid');
    $quantity = $this->input->post('qtde');

    $total = count($quantity);

    for($i=0; $i<$total; $i++){
        $update = array('rowid' => $value[$i], 'qtde' => $quantity[$i]);
        $this->cart->update($update);
    }
    redirect('shop');  
}
  • Thanks rray worked perfectly!! :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.