【问题标题】:Codeigniter - Shopping Cart: Won't let me get hidden field Row IDsCodeigniter - 购物车:不会让我获得隐藏字段行 ID
【发布时间】:2011-10-14 01:16:28
【问题描述】:

我正在尝试在我的购物车上获得编辑功能。 我希望它基本上转到一个名为 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);

上面的sn-p输出:

Array ( [1] => Array ( [qty] => [rowid] => ) [2] => Array ( [qty] => [rowid] => ) [3] => Array ( [qty] => [rowid] => ) ) 

这很奇怪,因为它似乎将“$i.'[qty]'”从前几行映射到正确的表单验证,但不会从隐藏字段中映射正确的数据。

请帮忙!

【问题讨论】:

    标签: php codeigniter shopping-cart


    【解决方案1】:

    您正在生成基于数组的表单名称。例如

    &lt;input type="hidden" name="1[rowid]" value="1starrthing" /&gt;,使您隐藏的表单值在帖子数据中显示为子数组,如下所示:

    [1] => Array
        (
            [rowid] => 1starrthing
            [qty] => 1
        )
    
    [2] => Array
        (
            [rowid] => 1starrthing222
            [qty] => 1
        )
    
    [3] => Array
        (
            [rowid] => 1starrthing3333
            [qty] => 1
        )
    

    因此,除非您要这样做,否则请尝试将隐藏的表单名称更改为 rowid[],这将为您提供如下所示的 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); ?>
    

    如需更多调试帮助,请尝试打开分析;这将为您提供有关帖子/查询和其他内容的更多信息。

    视图文件底部:

    $this-&gt;output-&gt;enable_profiler(TRUE);

    【讨论】:

    • 似乎很明智,更改名称似乎可以让我更改购物车。为什么这么多示例代码sn-ps写成这样呢?
    【解决方案2】:

    我不确定这是否与它有关,但在您的 set_rules 中,$i.'[qty]' 似乎关闭了。如果您尝试引用数组中的键,那么它需要在括号内加上引号,就像您的代码稍后所做的那样:即$i."['qty']" 或类似的东西?没有把握。关键是,我认为这就是代码中断开连接的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多