Cakephp将复选框数组传递给另一个控制器

时间:2013-03-20 16:40:16

标签: php arrays cakephp cakephp-2.0

我要做的是为用户提供一种方法来检查清单中的多个产品。

我的产品索引页面(列出要检出的所有可用产品)如下所示:

<?php echo $this->Form->create('multi');?>
<?php foreach ($products as $product): ?>
<tr class="hovertable">
    //All the fields go here
    <td style="cursor: default">
        <?php echo $this->Html->link($this->Html->image('tr/Checkouts_Add.png') . " " . __('Checkout'), array('controller' => 'Checkouts','action' => 'add', $product['Product']['id']), array('escape' => false, 'class' => 'button')); ?>
        <?php echo $this->Html->link($this->Html->image('tr/Edit.png'), array('action' => 'edit', $product['Product']['id']), array('escape' => false)); ?>
        <?php echo $this->Form->postLink($this->Html->image('tr/Delete.png'), array('action' => 'delete', $product['Product']['id']), array('escape' => false), __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
        <?php echo $this->Form->input('Product.id.'.$product['Product']['id'] ,
            array('label' => false,
                  'type' => 'checkbox',
                  'id'=>'listing_'.$product['Product']['id'])); ?>
    </td>
</tr>
<?php endforeach; ?>
<?php echo $this->Form->submit(__('Submit'));?>

然后在我的结帐控制器中我添加了一个新功能来结帐多个项目,我希望这个表格由选中的产品填充

    public function multi($count = 1) {
    if($this->request->is('post')) {            
        foreach($this->request->data['Checkout'] as $data) {
            //Do not forget this line. you need to create new model for saving each time.
            if ($this->request->isPost()) {
                $this->Checkout->create();
                $this->Checkout->save($data);
            } else {
                $this->request->data['Checkout']['product_id'] = $productId;
            }
        }
        $this->redirect(array('action' => 'index'));
    }
    $products = $this->Checkout->Product->find('list');
    $users = $this->Checkout->User->find('list');
    $this->set(compact('products', 'users'));
    $this->set('count', $count);
}

如您所见,我试图添加帽子,我认为可能会有效,但产品索引页面中的“提交”按钮不执行任何操作。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我只是检查几次,至少我明白你失败的原因之一就是你的foreach里面有一些errorwarning因为我不知道你的价值产品数组我检查这个代码,它对我有用:

<?php $products=array('0'=>'11','1'=>'22','2'=>'333');
echo $this->Form->create('User');?>
<?php foreach ($products as $product): ?>
<tr class="hovertable">
    //All the fields go here
    <td style="cursor: default">

        <?php echo $this->Form->input('Product.id.'.$product,
            array('label' => false,
                  'type' => 'checkbox',
                  'id'=>'listing_'.$product)); ?>
    </td>
</tr>
 <?php
 endforeach;

echo $this->Form->end(__('Submit')); 

如果您没有错误或警告,我建议您在没有

的情况下进行检查
  

$这 - &GT;形状配合&GT; postLink