I’m trying to get edit functionality on my Shopping cart.
I want it to be basically go to a page called users/view_cart, then the user selects whether they are only updating the cart(changing quantities) or are checking out (purchasing through a transaction).
I’m following the display the cart page provided at this location:
http://codeigniter.com/user_guide/libraries/cart.html
public function view_cart(){
$this->load->model('purchases_model');
$this->load->model('transactions_model');
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('action', 'action', 'required');
$num = $this->cart->total_items();
for($i = 1; $i <= $num; $i++){
$this->form_validation->set_rules($i.'[qty]',
'Quantity of the '.$i.'th element of the cart',
'required');
}
if($this->form_validation->run() === FALSE){
$data['title'] = 'View your cart!';
$this->load->view('templates/LI_header', $data);
$this->load->view('users/view_cart');
$this->load->view('templates/LI_footer');
} elseif($this->input->post('action') == 'checkout') {
$data['title'] = 'Buy cart!';
$id = $this->session->userdata('uid');
$transactionsData = array(
'amount' => $this->cart->format_number($this->cart->total())
);
$tid = $this->transactions_model->insert($transactionsData);
foreach($this->cart->contents() as $items){
$wsid = $items['id'];
$purchaseData = array(
'wsid' => $wsid,
'uid' => $id,
'tid' => $tid
);
$this->purchases_model->insert($purchaseData);
}
$this->load->view('templates/LI_header', $data);
$this->load->view('users/buy_cart_success');
$this->load->view('templates/LI_footer');
$this->cart->destroy();
} else {
$data['title'] = 'Edit Cart!';
/* Manual input of the rowid and new quantity work
$cartData = array('rowid' => 'c4ca4238a0b923820dcc509a6f75849b','qty'=>5);
$this->cart->update($cartData);
*/
/*
for($i = 1; $i <= $num; $i++){
$rowid = $this->input->post($i.'[rowid]');
$newQty = $this->input->post($i.'[qty]');
$cartData = array(
'rowid' => $rowid,
'qty' => $newQty
);
$this->cart->update($cartData);
}
print_r($formData);
$this->load->view('templates/LI_header', $data);
$this->load->view('users/edit_cart_success');
$this->load->view('templates/LI_footer');
}
}
Start of the view_cart view:
<?php echo form_open('users/view_cart'); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<?php echo validation_errors(); ?>
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
<th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"> </td>
<td class="right"><strong>Total</strong></td>
<td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
</br>
<?php echo form_radio('action','update',FALSE); ?> Update your cart
</br>
<?php echo form_radio('action','checkout', FALSE) ?> Check out your cart
</br>
<p><?php echo form_submit('users/view_cart', 'Process'); ?></p>
I tried writing some debugging code but the results I got were inconclusive.
In particular:
for($i = 1; $i <= $num; $i++){
$this->form_validation->set_rules($i.'[qty]','Quantity of the '.$i.'th element of the cart','required');
$formData[$i]['qty'] = $this->input->post(intval($i).'[qty]');
$formData[$i]['rowid'] = $this->input->post(intval($i).'[rowid]');
}
print_r($formData);
The above snippet outputs:
Array ( [1] => Array ( [qty] => [rowid] => ) [2] => Array ( [qty] => [rowid] => ) [3] => Array ( [qty] => [rowid] => ) )
Which is weird because it seems to map ” $i.'[qty]’ ” from the previous lines to the proper form validations but otherwise doesn’t map me the correct data from the hidden fields.
Help, please!
You are generating array based form names. e.g.
<input type="hidden" name="1[rowid]" value="1starrthing" />, causes your hidden form values to show in the post data as a sub array like so:So unless you’re going for that, try changing your hidden form name to
rowid[]which will give you an array of rowids like so:OR prepend your counter to the name like this:
name="rowid_.$i"which will give you:Also, it’s likely your values in your hidden inputs are empty unless for some reason your $items array has ‘qty’ as the key for each item. Try
For added debugging help, try turning profiling on; this will give you more info on post’s/queries and other stuff.
Bottom of view file:
$this->output->enable_profiler(TRUE);