无法从CodeIgniter中的循环内的表单元素获取数组数据

时间:2018-10-21 18:05:55

标签: php codeigniter

所以我有这种形式:

<?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)。我没有对此行为的解释,我在另一个模块中做了类似的实现,并且工作正常。我实际上已将功能由内而外,但问题仍然存在。我想知道我在做什么错。如果有人能指出我正确的方向,将不胜感激。

注意:上面是代码的简化版本,我删除了我认为对此问题不必要的内容。

1 个答案:

答案 0 :(得分:0)

您无法在php中回显数组。建议的方法是将数组转换为json对象,然后将json对象作为字符串存储在隐藏字段中,然后在提交表单时应解码json对象,然后将得到一个数组。