dtqu72830 2017-03-28 05:13
浏览 36
已采纳

如何在codeigniter中插入具有多个无线电名称的单选按钮值?

I'm trying to making online quiz with codeigniter where there is a dynamic quiz with some subject. User can add the question and the multiple choice answer in each question. How can i get the value of each answer in each multiple choice answer on every question?

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

and this is the code looks like when i try to inspect :

            <input type="radio" name="jawaban1" value="A">A.Hamster</input><br>
            <input type="radio" name="jawaban1" value="B">B.Tiger</input><br>
            <input type="radio" name="jawaban1" value="C">C.Shark</input><br>
            <input type="radio" name="jawaban1" value="D">D.Lion</input><br>
            <br><br>

            <input type="radio" name="jawaban2" value="A">A.was</input><br>
            <input type="radio" name="jawaban2" value="B">B.have</input><br>
            <input type="radio" name="jawaban2" value="C">C.am</input><br>
            <input type="radio" name="jawaban2" value="D">D.is</input><br>
            <br><br>
                <button class="btn btn-primary" type="submit">Submit</button>

Here is my Controller looks like :

public function addKuisAnswer(){
    if($this->session->userdata('data_user')->level != 1){
        redirect('/c_user/login');
    }
    $id_soal_kuis = $_POST['id_soal_kuis'];
    $id_user = $this->session->userdata('data_user')->id_user;
    $answer = $_POST['jawaban']; //The Problem's here... I should put jawaban with id_soal. Because the answer named 'jawaban1','jawaban2' etc it will follow the id_soal. How can i insert it?
    $this->load->model('m_kuis');
    $this->m_kuis->createAnswerKuis($id_soal_kuis, $id_user, $answer);
     echo " <script>
                    alert('Jawaban Tugas Tersimpan!');
                    history.go(-2);
                    </script>"; 

}

Here is my model :

public function createAnswerKuis($id_soal_kuis, $id_user, $answer){
    $data = array(
        'id_soal_kuis'=>$id_soal_kuis,
        'id_user' => $id_user,
        'jawaban_kuis' => $answer            
    );
    $this->db->set('tgl_jawab', 'NOW()', FALSE);
    $this->db->insert('jawab_kuis', $data);
}

I want the table looks like this :This is the picture of database table i made.I want to insert the data like this.

The problem is in Controller. How can my Controller post the value of radio button name="jawaban<?=$row->id_soal?>"in database?

  • 写回答

5条回答 默认 最新

  • dongqiao1151 2017-03-29 05:51
    关注

    i hope this little change will helps you

     public function addKuisAnswer(){
            if($this->session->userdata('data_user')->level != 1){
                redirect('/c_user/login');
            }
            $id_soal_kuis = $_POST['id_soal_kuis'];
            $id_user = $this->session->userdata('data_user')->id_user;
    
            $this->load->model('m_kuis');
            $i=1;
        while(isset($_POST['jawaban'.$i]))
        {
             $answer = $_POST['jawaban'.$i];
    //         var_dump($answer);
     $this->m_kuis->createAnswerKuis($id_soal_kuis, $id_user, $answer);
             $i++;
        }
    
             echo " <script>
                            alert('Jawaban Tugas Tersimpan!');
                            history.go(-2);
                            </script>"; 
    
        }
    

    you can change radio button name in sequential order(because there is no need of id appending in radio button)

     <?php 
    $i=1;
        foreach($soalKuis as $row){?>
                <?php $jawab_array = array($row->pil_a,$row->pil_b,$row->pil_c,$row->pil_d);
                ?>
                <p><?=$row->soal?>
        <input type="hidden" value=<?php echo $row->id_soal?> name='id_soal'<?= $i ?>/></p>
                <input type="radio" name="jawaban<?=$i?>" value="A">A.<?=$jawab_array[0]?></input><br>
                <input type="radio" name="jawaban<?=$i?>" value="B">B.<?=$jawab_array[1]?></input><br>
                <input type="radio" name="jawaban<?=$i?>" value="C">C.<?=$jawab_array[2]?></input><br>
                <input type="radio" name="jawaban<?=$i?>" value="D">D.<?=$jawab_array[3]?></input><br>
                <input type="hidden" name="id_soal_kuis" value="<?=$row->id_soal?>"></input>
                <br><br>
           <?php $i++;} ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路