insert radio value multiple data codeigniter in da

2019-07-15 01:44发布

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?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-15 01:56

I think this may helps 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 and implode

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

     $i++;
}
$answer = implode(',',$answer_array);
查看更多
三岁会撩人
3楼-- · 2019-07-15 02:00

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

}
查看更多
Juvenile、少年°
4楼-- · 2019-07-15 02:02

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.

查看更多
登录 后发表回答