2013-04-23 47 views
0

我試圖通過選中下拉菜單中的複選框來更新學生類的列表。我選擇了四個複選框。因此,調試正確顯示248,268,244,1220。但由於「classroom_id」,我得到了一個錯誤Warning (2): Illegal string offset 'id' [APP/controllers/customers_controller.php, line 728]。有沒有反正只顯示選定的複選框ID列表?cakephp 1.2多個更新複選框與下拉菜單

我試圖通過選擇複選框來更新多個記錄/行。我如何將多個選定的ID發送給控制器?

任何幫助將不勝感激。

<?php echo $form->input('classroom_id', array('type' => 'select','empty' => '-- Select --','label' => false,'style'=>'width:254px;', 'options' => $classesfiltered,'validate'=>'required:true','div'=>'formfield')); ?> 

<?php foreach ($students as $student) { ?> 
<?php echo $form->input('Customer.'.$student['Customer']['id'].'.id', array('type' => 'checkbox', 'id' => "admin_checkbox_".$student['Customer']['id'], 'label' => false)); ?> 
<?php } ?> 

控制器

foreach($this->data['Customer'] as $key => $item) { 
    if ($item['id']) { 
      Debugger::Dump($this->data['Customer'][$key]); 
    } 
} 

輸出

Warning (2): Illegal string offset 'id' [APP/controllers/customers_controller.php, line 728] 

"classroom_id" 

248 

268 

244 

1220 

回答

1

爲什麼你得到5個值,而不是原因是4因爲classroom_id輸入:

<?php echo $this->Form->input('classroom_id', array('type' => 'select','empty' => '-- Select --','label' => false,'style'=>'width:254px;', 'options' => $classesfiltered,'validate'=>'required:true','div'=>'formfield')); ?> 

既然你不包括在輸入的名稱模型的名稱(例如, Customer.classroom_id),它假定您想將其包含在當前模型中(當然,假設您沒有在表單元素中手動指定模型)。

這正確返回5個值。

但是,在您的控制器中,您假設您要獲取的唯一數據是複選框而不是選擇框。您正在循環所有「客戶」表單數據,而不僅僅是複選框。

有2種方式,我們可以解決這個問題:

選項#1:(推薦)修改你的表單輸入,然後更新您的循環

<?php 
echo $this->Form->input('Customer.classroom_id', array('type' => 'select','empty' => '-- Select --','label' => false,'style'=>'width:254px;', 'options' => $classesfiltered,'validate'=>'required:true','div'=>'formfield')); 

foreach ($students as $student) { 
    echo $this->Form->input('Customer.id.'.$student['Customer']['id'], array('type' => 'checkbox', 'id' => "admin_checkbox_".$student['Customer']['id'], 'label' => false)); 
} 
?> 

注:我切換輸入的第一個參數從'Customer.'.$student['Customer']['id'].'.id''Customer.id.'.$student['Customer']['id'] 這允許您訪問數組中的POST數據。生成的表單輸入的樣子(其中<student id>爲您的實際學生證):

<div class="input checkbox"> 
    <input type="hidden" name="data[Customer][id][<student id>]" id="admin_checkbox_<student id>_" value="0"> 
    <input type="checkbox" name="data[Customer][id][<student id>]" id="admin_checkbox_<student id>" value="1"> 
</div> 

然後,您可以通過每個複選框,在您的控制器分離,這應該是這個樣子的循環:

foreach ($this->data['Customer']['id'] as $id => $checked) { 
    if ($checked) { 
     Debugger::Dump($this->data['Customer'][$id]); 
    } 
} 

選項推薦使用#1,因爲它確保您的數據正確分離。然後你可以循環更快的數據。

選項#2:另一種選擇,我不建議,是通過所有的數據使用isset()函數進行循環,但只有在你想

foreach ($this->data['Customer'] as $key => $item) { 
    if (isset($item['id']) && $item['id']) { 
     Debugger::Dump($this->data['Customer'][$key]); 
    } 
} 
+0

你是明星! – DANLEE 2013-04-24 05:41:54

0

如果你確定該警告是不重要的使用error_reporting(E_ERROR);

+0

數據採取行動,但它應該只返回4值。目前它在每個循環中返回4個帶有classroom_id的檢查值。 – DANLEE 2013-04-23 05:25:28

0
Instead of creating single single check boxes you should use proper format of cakephp. Please check below: 

echo $this->Form->input('Model.field', array(
    'type' => 'select', 
    'multiple' => 'checkbox', 
    'options' => $students 
    ) 
)); 

In above code $students is holding your check boxes values like [0]=male, [1]=female. 

In your controller, you will one value same like you get value of textbox. 
+0

嗨阿迪亞。我不是在尋找多個複選框。我試圖通過選擇複選框來更新多個記錄/行。謝謝。我怎麼能發送多個選定的ID到控制器? – DANLEE 2013-04-23 22:33:08

相關問題