所以我有这个形式:
<?php echo flash_message_success('status_msg'); ?>
<?php echo form_open('students/mark_attendance/'.$session.'/'.$term.'/'.$class_id); ?>
<input type="checkbox" id="toggle_all" data-toggle="toggle" data-on="All Present" data-off="All Absent" data-onstyle="success" data-offstyle="danger" />
<?php
$i = 0;
foreach ($students as $s) {
$student_id = $s->id; ?>
<input type="hidden" name="student_id[]" value="<?php echo $student_id; ?>" />
<input class="bulk_select_present" type="radio" name="status[<?php echo $i; ?>]" value="Present" />Present
<input class="bulk_select_absent" type="radio" name="status[<?php echo $i; ?>]" value="Absent" />Absent
<input type="date" class="form-control" name="date" value="<?php echo set_value('date'); ?>" required />
<button class="btn btn-success" type="submit">Mark Attendance</button>
<?php echo form_close() //mark_attendance ?>
这是处理我的表单的函数:
public function attendance($session, $term, $slug) {
$class_details = $this->common_model->get_class_details_by_slug(school_id, $slug);
$page_title = 'Attendance: ' . $class_details->class;
$this->admin_header($page_title, $page_title);
$data['session'] = $session;
$data['term'] = $term;
$data['class_id'] = $class_details->id;
$data['slug'] = $slug;
$data['students'] = $this->common_model->get_students_list_by_class($class_details->id);
$this->load->view('admin/students/attendance', $data);
$this->admin_footer();
}
public function mark_attendance($session, $term, $class_id) {
$this->form_validation->set_rules('date', 'Date', 'trim|required');
$this->form_validation->set_rules('student_id[]', 'Student ID', 'trim');
$student_id = $this->input->post('student_id', TRUE);
$status = $this->input->post('status', TRUE);
$date = $this->input->post('date', TRUE);
$slug = $this->common_model->get_class_details($class_id)->slug;
if ($this->form_validation->run()) {
for ($i = 0; $i < count($student_id); $i++) {
$d_id = $student_id[$i];
$d_status = $status[$i];
//check if student was marked absent or absent
if ($d_status == 'Present') {
$this->students_model->mark_student_present($session, $term, $class_id, $d_id);
} else {
$this->students_model->mark_student_absent($session, $term, $class_id, $d_id);
}
}
$this->session->set_flashdata('status_msg', "Attendance marked successfully");
redirect('students/attendance/'.$session.'/'.$term.'/'.$slug);
} else {
$this->attendance($session, $term, $slug); //reload page with validation errors
}
}
问题是,数据不会提交到数据库(我的模型工作正常,我测试了它(。一些检查显示$student_id
没有解析为数组(我尝试使用 is_array 函数进行检查,它返回了 false(。我对这种行为没有解释,我在另一个模块中做了类似的实现,它工作正常。我实际上已经将功能翻了个底朝天,但问题仍然存在。我想知道我做错了什么。如果有人能指出我正确的方向,将不胜感激。
注意:以上是代码的简化版本,我删除了我认为这个问题不需要的内容。
你不能在PHP 中回显数组。建议的方法是,您应该将数组转换为 json 对象,然后将 json 对象存储为隐藏字段中的字符串,然后在提交表单时应该解码 json 对象,然后将获得一个数组。