Insert radio link value in multiple lines into database in one line?

I have an online quiz project, I want the answer to be saved as in a database

+-----------+-------------+---------+-------------+
| id_answer | id_student  | id_kuis |   answer    |
+-----------+-------------+---------+-------------+
|         1 | 99999874465 |       7 | A,B,D,A,C,B |
+-----------+-------------+---------+-------------+

      

this poll is multiple choice. So how can I insert response data like this.

here is my view

 <?php echo form_open('c_kuis/addKuisAnswer',$atribut); ?>
                        <?php foreach($soalPG as $row){?>
                        <?php $jawab_array = array($row->pil_a,$row->pil_b,$row->pil_c,$row->pil_d);?>
                        <p><?=$row->no_soal?>.<?=$row->soal?></p>
                        <input type="hidden"name="id_soal<?=$row->id_soal_pg?>" value="<?=$row->id_soal_pg?>"></input>
                        <input type="radio" name="jawaban<?=$row->no_soal?>" value="A"> A. <?=$jawab_array[0]?></input><br>
                        <input type="radio" name="jawaban<?=$row->no_soal?>" value="B"> B. <?=$jawab_array[1]?></input><br>
                        <input type="radio" name="jawaban<?=$row->no_soal?>" value="C"> C. <?=$jawab_array[2]?></input><br>
                        <input type="radio" name="jawaban<?=$row->no_soal?>" value="D"> D. <?=$jawab_array[3]?></input><br>
                        <br><br>
                    <?php } ?>
                    <input type="hidden" name="kuis_id" value="<?php echo $kuisPG->id_ks;?>"></input>
                    <button class="btn btn-primary submit" id= "submit" type="submit">Submit</button>
                    <?php echo form_close(); ?>
                    </div>

      

and here is my controller

public function addKuisAnswer(){
        $kuis_id = $_POST['kuis_id'];
        $id_user = $this->session->userdata('data_user')->no_id;
        $i=1;
        while(isset($_POST['jawaban'.$i]))
         {
            $answer = $_POST['jawaban'.$i];
         }
        $this->load->model('m_kuis');
        $this->m_kuis->answer_PG($id_user,$kuis_id,$answer);
         echo " <script>
                        alert('answer saved!');
                        history.go(-2);
                        </script>"; 

    }

      

How can I change my controller to input data as above example table?

+2


source to share


3 answers


I think this might help you

public function addKuisAnswer(){
    $kuis_id = $_POST['kuis_id'];
    $id_user = $this->session->userdata('data_user')->no_id;
    $i=1;
    while(isset($_POST['jawaban'.$i]))
     {
       if($i == 1)
         {
           $answer = $_POST['jawaban'.$i];
          }
        else{
        $answer. =','. $_POST['jawaban'.$i];
       }
      $i++;
     }
    $this->load->model('m_kuis');
    $this->m_kuis->answer_PG($id_user,$kuis_id,$answer);
     echo " <script>
                    alert('answer saved!');
                    history.go(-2);
                    </script>"; 

}

      



We have another solution using explode

andimplode

while(isset($_POST['jawaban'.$i]))
{
     $answer_array[] = $_POST['jawaban'.$i];

     $i++;
}
$answer = implode(',',$answer_array);

      

+1


source


Since there will only be one answer for your question, the name for your radio stream should be the same for all 4 parameters, so the user can only choose one answer and you can easily get it in the post request. and the rest of your code looks right.



+1


source


Try this code:

public function addKuisAnswer(){
    $kuis_id = $_POST['kuis_id'];
    $id_user = $this->session->userdata('data_user')->no_id;
    $i=1;
    while(isset($_POST['jawaban'.$i]))
     {
        $answer = $_POST['jawaban'.$i];
     }
    $this->load->model('m_kuis');

    // add this line to combine all the array values as a string
    $answer = implode(",",$answer);

    $this->m_kuis->answer_PG($id_user,$kuis_id,$answer);
     echo " <script>
                    alert('answer saved!');
                    history.go(-2);
                    </script>"; 

}

      

-1


source







All Articles