Raheel Khan
Raheel Khan

Reputation: 494

how to get multiple radio inputs and store them in database?

NOTE: this is not a duplicate of any question, i need answer to this specific problem.

below is my view code

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

This is my controller where i want to get answers from view.

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?
}

this is my questions model and controller.

from this controller i am selecting the questions from database to ask.

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

    }

i do know how to get a single input from the view like

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

and then pass it to the model. but i am confused in the above code, how to get data from the multiple radio's.

Upvotes: 1

Views: 149

Answers (1)

Raheel Khan
Raheel Khan

Reputation: 494

Okay I figured it out with help of unknown friend.

this is how i need to write my controller.

for now i am statically putting everything except the "marks_answer" field, which was my question that how to do it dynamically.

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


        }

Upvotes: 1

Related Questions