Codeigniter - 购物车:不会让我获得隐藏字段行 ID
我正在尝试在我的购物车上获取编辑功能。 我希望它基本上进入一个名为 users/view_cart 的页面,然后用户选择是仅更新购物车(更改数量)还是结账(通过交易购买)。
我正在关注此位置提供的显示购物车页面:
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');
}
}
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]' ”从前面的行映射到正确的表单验证,但没有从隐藏字段映射我的正确数据。
请帮忙!
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">lt;?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">lt;?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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在生成基于数组的表单名称。例如
,使您的隐藏表单值在发布数据中显示为子数组,如下所示:
因此,除非您打算这样做,否则请尝试将隐藏表单名称更改为
rowid[]
,这将为您提供一个 rowid 数组,如下所示:或者将计数器添加到名称前面,如下所示:
name="rowid_.$i"
这将为您提供:此外,隐藏输入中的值可能为空,除非由于某种原因您的 $items 数组将“qty”作为每个项目的键。尝试
要添加调试帮助,请尝试打开分析;这将为您提供有关帖子/查询和其他内容的更多信息。
视图文件底部:
$this->output->enable_profiler(TRUE);
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);
我不确定这是否与此有关,但在您的 set_rules 中, $i.'[qty]' 似乎已关闭。如果您尝试引用数组中的键,那么它需要在括号内包含引号,就像您的代码稍后所做的那样:即
$i."['qty']"
或者类似的东西?没有把握。重点是,我认为这就是代码中断开连接的地方。I'm not sure if this has anythign to do with it, but in your set_rules, the $i.'[qty]' seems off. If you're trying to reference the key in the array, then it'd need to have quotes inside the brackets, just like your code does later on: i.e.
$i."['qty']"
or something like that? Not sure. Point is, I think that's where the disconnect is in the code.