CodeIgniter Forums
Cart Class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Cart Class (/showthread.php?tid=51561)

Pages: 1 2


Cart Class - El Forum - 05-09-2012

[eluser]the_unforgiven[/eluser]
Hi all,

Currently working with the shopping cart class and ive got working the insert in cart and to show the cart items but when i do update of qty nothing happens...

Here's the code:
Code:
function update_cart()
{
  $update = array(
          'rowid' => $this->input->post('rowid'),
          'qty'   => $this->input->post('qty')
  );

  $this->cart->update($update);
  $this->session->set_flashdata('message','Your shopping cart has been updated!');
    redirect('home/cart','refresh');
}

Just been referencing to the manual, so nothing used in terms of anyone elses classes or libs.
Help greatly appreciated.


Cart Class - El Forum - 05-09-2012

[eluser]craig.hoog[/eluser]
[quote author="the_unforgiven" date="1336563664"]
Code:
function update_cart()
{
  $update = array(
          'rowid' => $this->input->post('rowid'),
          'qty'   => $this->input->post('qty')
  );

  $this->cart->update($update);
  $this->session->set_flashdata('message','Your shopping cart has been updated!');
    redirect('home/cart','refresh');
}
[/quote]

Everything looks good at a quick glance.
Can you output the Cart rowid and make sure your input->post('rowid') is correct?


Cart Class - El Forum - 05-09-2012

[eluser]the_unforgiven[/eluser]
I did
Code:
<?php var_dump($this->input->post('rowid')); ?>
which does says "bool(false)"

but if i output the whole $items array the rowid is set! So not sure why!


Cart Class - El Forum - 05-09-2012

[eluser]craig.hoog[/eluser]
I think you need to post the code you used to build the actual Update form.

Quote:The main advantage of using the provided functions ($this->input->post('something')) rather
than fetching an item directly ($_POST['something']) is that the functions will
check to see if the item is set and return false (boolean) if not.

It would appear that it's not being sent through properly as a POST variable.
Is your form method set to POST for sure? The name="" field of the input is "rowid"?

I can help more if I see the form code. I think you might be confusing the Items array with the POST variables.


Cart Class - El Forum - 05-09-2012

[eluser]the_unforgiven[/eluser]
Basically using what is stated in the manual:

Code:
<?php echo form_open('home/update_cart'); ?>
<?php if ($this->session->flashdata('message')) { ?>
<div class="success">&lt;?php echo $this->session->flashdata('message'); ?&gt;</div>
&lt;?php } ?&gt;

<table cellpadding="6" cellspacing="1" border="0">

<tr>
   <th>QTY</th>
   <th>Item Description</th>
   <th>Item Price</th>
   <th>Sub-Total</th>
</tr>
&lt;?php $i = 1; ?&gt;

&lt;?php foreach ($this->cart->contents() as $items): ?&gt;

  &lt;?php echo form_hidden($i.'[rowid]', $items['rowid']); ?&gt;

  <tr>
    <td>&lt;?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?&gt;</td>
    <td>
   &lt;?php echo $items['name']; ?&gt;

    &lt;?php if ($this->cart->has_options($items['rowid']) == TRUE): ?&gt;

     <p>
      &lt;?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?&gt;

       <strong>&lt;?php echo $option_name; ?&gt;:</strong> &lt;?php echo $option_value; ?&gt;<br />

      &lt;?php endforeach; ?&gt;
     </p>

    &lt;?php endif; ?&gt;

    </td>
    <td>&lt;?php echo $this->cart->format_number($items['price']); ?&gt;</td>
    <td>&pound;&lt;?php echo $this->cart->format_number($items['subtotal']); ?&gt;</td>
  </tr>

&lt;?php $i++; ?&gt;

&lt;?php endforeach; ?&gt;

  &lt;?php //var_dump($this->input->post('rowid')); ?&gt;

<tr>
   <td></td>
   <td><strong>Total</strong></td>
   <td>&pound;&lt;?php echo $this->cart->format_number($this->cart->total()); ?&gt;</td>
</tr>

</table>

<p>&lt;?php echo form_submit('', 'Update Cart'); ?&gt;</p>

Which then goes to what I originally posted.


Cart Class - El Forum - 05-09-2012

[eluser]the_unforgiven[/eluser]
Also this is what im getting on view source
Code:
&lt;input type="hidden" name="1[rowid]" value="a5bfc9e07964f8dddeb95fc584cd965d" /&gt;



Cart Class - El Forum - 05-09-2012

[eluser]craig.hoog[/eluser]
I see your issue. You're passing through rowids based on an array, but on the backend you're looking for just a generic
Code:
$_POST['rowid']

You will have to either loop through the rowid's on the backend and update them all, or simply isolate the one that was updated and reference it properly on the backend.

If you var_dump the entire $_POST, you will probably have:

Code:
$_POST[0]['rowid']
$_POST[1]['rowid']

Or something like that.



Cart Class - El Forum - 05-09-2012

[eluser]the_unforgiven[/eluser]
I did

var_dump($_POST) which outputted: array(0) { }


Cart Class - El Forum - 05-09-2012

[eluser]craig.hoog[/eluser]
[quote author="the_unforgiven" date="1336571922"]I did

var_dump($_POST) which outputted: array(0) { } [/quote]

Did you do this on the controller side or in the view?
You need to do it on the page where the form submits, so that the form has Posted the data. I also noticed your view had $this->input->post var_dump'd there too - that also would have had to take place in a controller in order to properly test for POST being passed through.



Cart Class - El Forum - 05-09-2012

[eluser]the_unforgiven[/eluser]
Apologise, i had put it in the view file: after putting it in the controller i get this:

array(1) { [1]=> array(2) { ["rowid"]=> string(32) "a5bfc9e07964f8dddeb95fc584cd965d" ["qty"]=> string(1) "2" } }