【问题标题】:how to get multiple radio inputs and store them in database?如何获取多个无线电输入并将它们存储在数据库中?
【发布时间】:2019-01-11 15:24:45
【问题描述】:

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

下面是我的查看代码

<?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");

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

【问题讨论】:

  • 您会得到所有发布的数据,如下所示:$data = $this-&gt;input-&gt;post()
  • 是这样,但不知道从上面的观点如何?

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


【解决方案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);


        }

【讨论】:

    猜你喜欢
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2011-09-03
    • 2021-12-03
    • 2012-02-22
    • 2021-07-31
    相关资源
    最近更新 更多