The reason why you're getting 5 values instead of 4 is because of the classroom_id input:
<?php echo $this->Form->input('classroom_id', array('type' => 'select','empty' => '-- Select --','label' => false,'style'=>'width:254px;', 'options' => $classesfiltered,'validate'=>'required:true','div'=>'formfield')); ?>
Since you don't include the model's name in the input's name (e.g. Customer.classroom_id), it makes an assumption that you want to include it in the current model (assuming you didn't manually specify a model in the form element, of course).
This is correctly returning 5 values.
In your controller, however, you're assuming that the only data you're going to get is the checkboxes and not the select box. You're looping through ALL the "Customer" form data, not just the checkboxes.
There are 2 ways we could fix this:
Option #1: (Recommended) Modify your form input, and then update your loop
<?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));
}
?>
Note: I switched the input's first parameter from 'Customer.'.$student['Customer']['id'].'.id'
to 'Customer.id.'.$student['Customer']['id']
This allows you to access your POSTed data in an array. The resulting form input will look like (where <student id>
is your actual 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>
You can then loop through each checkbox separate in your controller, which should look something like:
foreach ($this->data['Customer']['id'] as $id => $checked) {
if ($checked) {
Debugger::Dump($this->data['Customer'][$id]);
}
}
Option #1 is recommended, because it ensures your data is properly separated. You can then loop through just the data you need which is faster.
Option #2: The other option, which I don't recommend, is to use isset() to loop through all data but only act on the data you want
foreach ($this->data['Customer'] as $key => $item) {
if (isset($item['id']) && $item['id']) {
Debugger::Dump($this->data['Customer'][$key]);
}
}