Advertisement
eyuprog

Soal2

Aug 27th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. Pada Viewnya gini
  2. <?php
  3. $data_soal=array(
  4.     2=>array(
  5.         'judul'=>'Soal 1',
  6.         'ganda'=>array(
  7.             'a'=>'Pilihan 1 A',
  8.             'b'=>'Pilihan 1 B',
  9.             'c'=>'Pilihan 1 C',
  10.             'd'=>'Pilihan 1 D',
  11.         )
  12.     ),
  13.     4=>array(
  14.         'judul'=>'Soal 2',
  15.         'ganda'=>array(
  16.             'a'=>'Pilihan 2 A',
  17.             'b'=>'Pilihan 2 B',
  18.             'c'=>'Pilihan 2 C',
  19.             'd'=>'Pilihan 2 D',
  20.         )
  21.     ),
  22. );
  23. echo form_open($url.'simpan');
  24. ?>
  25.     <input type="hidden" name="user" value="1"/> <!-- User ID nya -->
  26.     <?php
  27.     $nomor=0;
  28.     foreach($data_soal as $k=>$v) // $k ID Soal
  29.     {
  30.         $nomor+=1;
  31.         echo $v['judul'];
  32.         echo '<br/>';
  33.         foreach($v['ganda'] as $gk=>$gv)
  34.         {
  35.             //$k index soal, $gk pilihan
  36.             ?>
  37.             <input type="radio" name="jawaban[<?=$k;?>]" value="<?=$gk;?>"/> <?=$gv;?> <br/>
  38.             <?php
  39.         }
  40.         echo '<p>&nbsp;</p>';
  41.     }
  42.     ?>
  43.     <button type="submit">Simpan</button>
  44. <?php
  45. echo form_close();
  46. ?>
  47.  
  48. //Pada Controller
  49. function simpan()
  50.     {
  51.         //Struktur Tablenya gini
  52.         /*
  53.         Table jawaban
  54.         user NOT NULL
  55.         soal NULL
  56.         jawaban NULL
  57.         */
  58.         $user=$this->input->post('user',TRUE);
  59.         $d=array(
  60.             'user_id'=>$user
  61.         );
  62.         $this->db->insert('jawaban',$d);
  63.        
  64.         $jawaban=$this->input->post('jawaban',TRUE);
  65.         foreach($jawaban as $soalID=>$jawabanID)
  66.         {
  67.             $s=array('user'=>$user);
  68.             $djawaban=array(
  69.                 'soal'=>$soalID,
  70.                 'jawaban'=>$jawabanID
  71.             );
  72.             $this->db->update('jawaban',$djawaban,$s);
  73.         }
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement