PHP Codeigniter中的foreach循环中的单选按钮值

时间:2018-07-02 12:55:27

标签: php codeigniter

我试图在foreach循环中创建radio类型的输入元素,并在单击Submit按钮时将值保存在数据库中。我正在使用Codeigniter框架。

我的观点:

<?php echo form_open('students_report/produce_aptitude_score/'.$y->id); ?>

    <table>
        <tbody>
            <?php
            $i = 1;
            foreach ($aptitudes as $p) { ?>

                <input type="hidden" name="aptitude[]" value="<?php echo $p->aptitude; ?>" />
                <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $p->aptitude; ?></td>

                    <td>
                        <div class="form-group">
                            <input class="form-control" type="radio" name="score[<?php echo $i; ?>]" value="1" />
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <input class="form-control" type="radio" name="score[<?php echo $i; ?>]" value="2" />
                        </div>
                    </td>
                </tr>
                <?php $i++; //increment index
             } //endforeach ?>
        </tbody>
    </table>

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

<?php echo form_close(); ?> 

我的控制器

public function produce_aptitude_score($id) { 
        $y = $this->common_model->get_student_details_by_id($id);
        $this->form_validation->set_rules('aptitude[]', 'Aptitude', 'trim');

        $aptitude = $this->input->post('aptitude', TRUE);
        $score = $this->input->post('score', TRUE);

        if ($this->form_validation->run()) {
            for ($i = 0; $i < count($aptitude); $i++) {
                $d_aptitude = $aptitude[$i];
                $d_score = $score[$i];
                $query = $this->students_report_model->check_aptitude_score_exists($id, $d_aptitude);
                if ($query->num_rows() == 0) { //data does not exists, do insert
                    $this->students_report_model->insert_aptitude_score($id, $d_aptitude, $d_score);
                } else { //data already exists, do update
                    $this->students_report_model->update_aptitude_score($id, $d_aptitude, $d_score); 
                } 
            }
            $this->session->set_flashdata('status_msg', "Aptitude score submitted successfully for {$y->first_name}");  
            redirect($this->agent->referrer());
        } else {
            $this->produce_report($id); //form validation fails, reload page with errors
        }
    }

我的模型

public function insert_aptitude_score($id, $aptitude, $score) {
        $y = $this->common_model->get_student_details_by_id($id);
        $data = array(
            'admission_id' => $y->admission_id, 
            'aptitude' => $aptitude, 
            'score' => $score, 
            'session' => current_session, 
            'term' => current_term, 
        );
        return $this->db->insert('aptitude_scores', $data); 
    } 


    public function update_aptitude_score($id, $aptitude, $score) {
        $y = $this->common_model->get_student_details_by_id($id);
        $query = $this->check_aptitude_score_exists($id, $aptitude);
        $result_id = $query->row()->id;
        $data = array(
            'aptitude' => $aptitude, 
            'score' => $score, 
        );
        $this->db->where('id', $result_id); 
        return $this->db->update('aptitude_scores', $data); 
    } 

提交时,为什么会出现以下错误?

  

PHP:未定义偏移量:0

     

数据库:列“得分”不能为空

0 个答案:

没有答案