将数据库中的多个数据codeigniter的无线电值插入一行?

时间:2017-05-29 05:30:47

标签: php mysql codeigniter codeigniter-2

我有一个测验在线项目我想在数据库中保存这样的答案

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

这个测验是多项选择。那么如何插入这样的答案数据呢。

这是我的观点

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

这是我的控制器

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

    }

如何编辑我的控制器以输入数据作为上面的示例表?

3 个答案:

答案 0 :(得分:1)

由于您的问题只有一个答案,因此所有4个选项的无线电广告名称应该相同,因此用户只能选择单个答案,您可以轻松地在帖子请求中获取。 其余代码看起来是正确的。

答案 1 :(得分:1)

我认为这可能有助于你

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

}

我们有另一个解决方案,使用explodeimplode

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

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

答案 2 :(得分:-1)

试试这段代码:

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

}