Welcome Guest, Not a member yet? Register   Sign In
Updating quantity of a specific item in cart?
#1

[eluser]umbongo[/eluser]
I am trying to make my code such that the number of items in the cart can be updated from within the cart. This is my code so far;

Code:
<?php echo form_open('basket/update/');
$i = 1; //item position within cart, autoincrements
foreach($this->cart->contents() as $items):
    echo form_hidden($i.'_rowid', $items['rowid']); //items['rowid'] is unique identifier;
    echo form_input(array(
        'name' => $i.'_qty',
        'value' => $items['qty'], //holds # of this item.
        'maxlength' => '3',
        'size' => '1'
      )); ?&gt;</td>

&lt;?php $i++;
endforeach;
echo form_submit('', 'Update Basket'); ?&gt;

and the basket/update being pointed to;
Code:
function update($rowid) {
    
        
        $this->cart->update(array(
            'rowid' => $rowid,
            'qty' => $items['qty']
        ));
        
        $this->load->view('basket_view');
    
    }

I am a little unsure regarding passing variables in CI. I think I have to pass $rowid in the URL @ form_open, but what if I am changing the value for multiple items at once?
I am also unsure how I would reference $items['rowid'] outside of the foreach loop (even if this isn't necessary).
Some guidance would be appreciated.
#2

[eluser]WanWizard[/eluser]
Don't pass values in the URI, unless it's a get operation. Use forms, and post the form.
In case of multiple items, use arrays as form element names, so you can post as much as you want.
#3

[eluser]umbongo[/eluser]
OK, well i was trying that above.

code as is gives 'Undefined variable: i' and does not update quantities.
#4

[eluser]cahva[/eluser]
In a nutshell

View:
Code:
$i = 1;

echo form_open('shopping_cart');

foreach($this->cart->contents() as $item):

    echo form_hidden('cart['.$item['id'].'][rowid]', $item['rowid']);
    
    // Name
    echo $item['name'];
    
    // Quantity
    echo form_input('cart['.$item['id'].'][qty]',$item['qty']);
    
    $i++;
    
endforeach;
echo form_submit('update','Update!');
echo form_close();

In controller to update you basically can do this:
Code:
if ($this->input->post('cart'))
{
    $this->cart->update($this->input->post('cart'));
}

Thats overly simplified but you'll get the idea.
#5

[eluser]umbongo[/eluser]
Thanks, works great!

How should I limit the size and maxlength of the input form above?

edit: i guess listed above is just the name part of the array in my OP.
#6

[eluser]Unknown[/eluser]
Thanks for the code guys,
Thi swas really advantage in my part.
Keep it up and Godspeed!
how to deal with depression




Theme © iAndrew 2016 - Forum software by © MyBB