Welcome Guest, Not a member yet? Register   Sign In
The Cart in Codeigniter 3
#1

New at the cart and struggling a little.

I am trying to send multiple updates through a multidimensional array from the view to the controller and I am having troubles, below is my view code:

Code:
<?php $i = 1; ?>
               <?php foreach ($this->cart->contents() as $items): ?>                
               <tr class="item first">
                   
                   <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
                   
                   <td class="thumb"><a href=""><img src="img/catalog/shopping-cart-thumb.jpg" alt="<?php echo $items['name']; ?>"/></a></td>
                   <td class="name"><a href=""><?php echo $items['name']; ?></a></td>
                   <td class="price"><?php echo $this->cart->format_number($items['price']); ?></td>
                   <td class="qnt-count">
                       <a class="incr-btn" href="#">-</a>
                       <?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '2', 'size' => '2')); ?>
                       <a class="incr-btn" href="#">+</a>
                   </td>
                   <?php $total = $this->cart->format_number($items['price'])*$items['qty']; ?>
                   <td class="total">QR <?php echo $total ?></td>
               </tr>
               <?php $i++; ?>
               <?php endforeach; ?>  

I have pretty much copied the code from the user guide but I am a little unsure on how to update the cart in the controller, I have started the below:

PHP Code:
function update_cart() {
 
       
        $data 
= array (
 
               'rowid' => ,
 
               'qty'   => ,
 
           );
 
       
$this
->cart->update($data);
redirect('/shop/cart''refresh');


I am a little unsure on how to create a multidimensional array and update all basket items.  I have it updating the last one but cant do them all.

Thanks for your help.
Reply
#2

Shouldn't each row have it's own form? Normaly you wouldn't update all cart items at the same time. Each row updates when an action has taken place. You could trigger an update manually with button clicks or when the input value changes.

But if you want to update all items at once you could try to create an array where the rowid is used as the key:

PHP Code:
// So use [] in the input names to have them posted as array values
<input name="qty[$items['rowid']" value="" /> 

Your post array should look something like this
PHP Code:
$post['qty'] = array('rowid-1'=>2,'rowid-2'=>3,'rowid-3'=>4); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB