2013-03-20 204 views
0

我正在嘗試的是爲用戶提供一種從庫存中檢出多個產品的方法。Cakephp將複選框數組傳遞給另一個控制器

我的產品索引頁(列出所有可用的產品被檢查出)看起來是這樣的:

<?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); 
} 

正如你看到的,我試圖添加帽子我想可能會奏效,但在產品索引頁什麼也不做Submit按鈕進行填充。任何幫助將不勝感激!

回答

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')); 

,如果您有任何錯誤或警告我建議你檢查不

$這個 - >形式 - > postLink

相關問題