我想在我的购物车上获得编辑功能。我希望它基本上是到一个名为users/view_cart的页面,然后用户选择他们是否只是更新购物车(更改数量)或结帐(通过交易购买)。
我遵循在此位置提供的显示购物车页面:
http://codeigniter.com/user_guide/libraries/cart.htmlpublic 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');
}
}
view_cart视图开始:
<?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>
我试着写一些调试代码,但我得到的结果是不确定的。特别是:
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);
上面的代码片段输出:
Array ( [1] => Array ( [qty] => [rowid] => ) [2] => Array ( [qty] => [rowid] => ) [3] => Array ( [qty] => [rowid] => ) )
这很奇怪,因为它似乎映射"$i"。'[qty]' "从前面的行映射到正确的形式验证,但不会从隐藏字段映射正确的数据。
帮助,谢谢!
您正在生成基于数组的表单名称。例如
<input type="hidden" name="1[rowid]" value="1starrthing" />
,使隐藏的表单值在post数据中显示为子数组,如下所示:
[1] => Array
(
[rowid] => 1starrthing
[qty] => 1
)
[2] => Array
(
[rowid] => 1starrthing222
[qty] => 1
)
[3] => Array
(
[rowid] => 1starrthing3333
[qty] => 1
)
所以,除非你想这么做,试着把你的隐藏表单名称改为rowid[]
这会给你一个数组,像这样:
[rowid] => Array
(
[0] => 1starrthing
[1] => 1starrthing222
[2] => 1starrthing3333
)
[1] => Array
(
[qty] => 1
)
[2] => Array
(
[qty] => 1
)
[3] => Array
(
[qty] => 1
)
或者在计数器前面加上这样的名称:name="rowid_.$i"
,这将给你:
[rowid_1] => 1starrthing
[1] => Array
(
[qty] => 1
)
[rowid_2] => 1starrthing222
[2] => Array
(
[qty] => 1
)
[rowid_3] => 1starrthing3333
[3] => Array
(
[qty] => 1
)
另外,很可能隐藏输入中的值是空的,除非出于某种原因,您的$items数组将'qty'作为每个项目的键。试着
<?php echo form_hidden('rowid[]', $items); ?>
要获得额外的调试帮助,请尝试打开分析;这将给你更多关于post/查询和其他东西的信息。
视图文件底部:
$this->output->enable_profiler(TRUE);
我不确定这是否与它有任何关系,但在您的set_rules中,$ I。"[qty]"似乎不太对劲。如果你试图引用数组中的键,那么它需要在括号内加上引号,就像你的代码一样:即$i."['qty']"
或类似的东西?不确定。关键是,我认为这就是代码中脱节的地方