在代码点火器的foreach循环中从表单中批量插入

时间:2018-06-25 09:54:53

标签: php codeigniter

我有一个视图,其中包含一个表单,该表单显示数据库中的所有主题,并带有输入框,用于为每个主题提供分数,然后单击按钮以一次提交数据到数据库。

        <?php echo form_open('teacher/submit_ca_score/'.$d->id); ?>

            <table class="table table-bordered cell-text-middle" style="text-align: left">
                <thead>
                    <tr>
                        <th class="w-10">S/N</th>
                        <th class="w-50">Subject</th>
                        <th class="w-15">CA Score</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $count = 1;
                    foreach ($subjects as $s) { 

                        $query = $this->teacher_model->check_ca_score_exists($d->id, $s->subject); ?>

                        <tr>
                            <td><?php echo $count; ?></td>
                            <td><?php echo $s->subject; ?></td>
                            <td>
                                <div class="form-group">
                                    <input type="hidden" name="subject[]" value="<?php echo $s->subject; ?>" />
                                </div>
                            </td>
                        </tr>

                        <?php $count++; ?>
                    <?php } ?>
                </tbody>
            </table>

            <div class="form-group">
                <button class="btn btn-success btn-lg">Submit</button>
            </div>

        <?php echo form_close() //submit_ca_score ?>

这是我的控制者:

public function submit_ca_score($id) { 
        $this->form_validation->set_rules('ca_score[]', 'CA Score', 'trim');
        $subjects = $this->input->post('subject', TRUE);
        $scores = $this->input->post('ca_score', TRUE);
        $y = $this->common_model->get_student_details_by_id($id);
        if ($this->form_validation->run()) {
            $data = array();
            foreach ($subjects as $key => $value) {
                $row = array();
                $row['admission_id'] = $y->admission_id;
                $row['subject'] = $value;
                $ca_score = "";
                foreach ($scores as $key => $value) {
                    $ca_score = $value;
                }
                $row['ca_score'] = $ca_score;
                $row['session'] = current_session;
                $row['term'] = current_term;
                $data[] = $row;
            }
            if ( ! empty($data) ) {
                $this->teacher_model->batch_insert_ca_score($id, $data); 
                $this->session->set_flashdata('status_msg', "{$subject} CA score submitted successfully for {$y->first_name}");
            } else {
                $this->session->set_flashdata('status_msg_error', "Error submitting CA score!");
            }
            redirect($this->agent->referrer());
        } else {
            $this->produce_report($id); //form validation fails, reload page with errors
        }
}

...以及模型:Teacher_model

public function batch_insert_ca_score($id, $data = array()) {
        $insert = $this->db->insert_batch('student_results', $data);    
        return $insert ? TRUE : FALSE;
}

批处理操作有效,但是未选择ca_score字段中的数据。我知道我做错了什么,但我无法弄清到底是什么。我已经尝试了几个小时,基本上是在玩,但似乎没有任何效果。我刚刚开始在代码点火器中尝试批处理操作。有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

尝试一下:-

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data);

https://www.codeigniter.com/userguide2/database/active_record.html