I have a quiz online project i want the answer save like this in database
+-----------+-------------+---------+-------------+
| id_answer | id_student | id_kuis | answer |
+-----------+-------------+---------+-------------+
| 1 | 99999874465 | 7 | A,B,D,A,C,B |
+-----------+-------------+---------+-------------+
this quiz is multiple choice. So how can i insert the data of answer like that.
here's 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's 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 edit my controller to input data as the example table above?
I think this may helps you
We have another solution,using
explode
andimplode
Try this code:
Since there will be only one answer for your question the name for your radio buttion should be same for all 4 options, so the user can select only single answer and you can easily get it in the post request. and rest of your code is looks correct.