Cakephp 1.2多个更新复选框与下拉菜单


cakephp 1.2 multiple update checkboxes with dropdown menu

我试图通过从下拉菜单中选择复选框来更新学生类列表。我选中了四个复选框。因此,调试正确显示248,268,244,1220。但我得到了一个错误Warning (2): Illegal string offset 'id' [APP/controllers/customers_controller.php, line 728]由于"classroom_id"。无论如何,只是显示选定的复选框id列表?

我试图通过选择复选框更新多个记录/行。如何向控制器发送多个选定的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

您获得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个值。

然而,在控制器中,您假设您将获得的唯一数据是复选框而不是选择框。您正在循环遍历所有"Customer"表单数据,而不仅仅是复选框。 有两种方法可以解决这个问题:

选项#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>是您的实际学生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]);
    }
}

如果您确定警告不重要,请使用error_reporting(E_ERROR);

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.