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.– rray
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!!
– Flavio Domingos
A normal person seems better in this situation I imagine
rowid
andqtde
be array with the same number of elements, then just hit the Dice and send to the bank, this kills the most internal foreach.– rray
Can you pass me how I can implement this, please
– Flavio Domingos
Someone else can help me!
– Flavio Domingos