如何获得多个无线电输入并将其存储在数据库中?

时间:2018-08-03 17:40:06

标签: codeigniter model-view-controller input radio-button codeigniter-3

注意:这不是任何问题的重复,我需要回答这个特定问题。

下面是我的查看代码

<?php 
    $index = 1;

    foreach ($questions as $ques): ?>
    <tr>
      <td><?php echo $index;?> </td>
      <td> <?php echo $ques->question;?> </br>
      <div class="custom-control custom-radio">

      <input class="custom-control-input" type="radio" id="sdisagree[<?php echo $index;?>]" name="q<?php echo $ques->id;?>" value="1" > 
      <label class="custom-control-label" for="sdisagree[<?php echo $index;?>]"> Strongly Disagree

      <input class="custom-control-input" type="radio" id="disagree[<?php echo $index;?>]" name="q<?php echo $ques->id;?>" value="2" >  
      <label class="custom-control-label" for="disagree[<?php echo $index;?>]">Disagree

      <input class="custom-control-input" id="slightlydisagree[<?php echo $index;?>]" type="radio" name="q<?php echo $ques->id;?>" value="3" > 
      <label class="custom-control-label" for="slightlydisagree[<?php echo $index;?>]">Slightly Disagree

      <input class="custom-control-input" id="slightlyagree[<?php echo $index;?>]" type="radio" name="q<?php echo $ques->id;?>" value="4" > 
      <label class="custom-control-label" for="slightlyagree[<?php echo $index;?>]">Slightly Agree

      <input class="custom-control-input" id="agree[<?php echo $index;?>]" type="radio" name="q<?php echo $ques->id;?>" value="5" >
      <label class="custom-control-label" for="agree[<?php echo $index;?>]"> Agree

      <input class="custom-control-input" id="sagree[<?php echo $index;?>]" type="radio" name="q<?php echo $ques->id;?>" value="6" > 
      <label class="custom-control-label" for="sagree[<?php echo $index;?>]">Strongly Agree
      </td>


    </tr>
    <?php 
    $index++;
    endforeach ;?>

这是我的控制器,我要在其中获取视图的答案。

public function get_answer(){
//i don't know what to write here to get answers from the fields. 
//am i required to use loops? if yes then how?
}

这是我的问题模型和控制器。

从此控制器中,我正在从数据库中选择要提问的问题。

public function survey_questions(){
        $this->load->model("csv_model");
        $data['survey'] = $this->csv_model->fetch_survey_questions();
        $data["category"] = $this->csv_model->fetch_categories();
        $this->load->view("survey_questions", $data);

    }

我确实知道如何从

之类的视图中获取单个输入

$ variable = $ this-> input-> post(“ fieldname”);

,然后将其传递给模型。但是我对上面的代码感到困惑,如何从多个无线电中获取数据。

1 个答案:

答案 0 :(得分:1)

好吧,我想通了,在一位不知名的朋友的帮助下。

这就是我需要写我的控制器的方式。

现在,我静态地放置了除“ marks_answer”字段以外的所有内容,这是我的问题,即如何动态地进行操作。

public function save_student_filled_questionaire(){

    $questions = $this->csv_model->fetch_questionaire();

        foreach($questions as $q){

        $data = array(
            'question_id' => $q->id, 
            'cat_id' => "1",
            'marks_answer' => $this->input->post('q'.$q->id),
            'student_id' => "1",
            'course_offered_id' => "1",
            'batch_id' => "1"

        );

        $this->db->insert('tbl_survey_answers', $data);


        }