pantes saja kamu bingung orang table kamu salah. Yang benar seperti ini:
tb_soal (
`id_soal` int(11) NOT NULL auto_increment,
`soal` text NOT NULL,
`id_gambar` int(4) NOT NULL,
PRIMARY KEY (`id_soal`)
)
tb_jawaban(
`id_jawaban` int(11) NOT NULL auto_incremenet,
`id_soal` int(11) not null,
`jawaban` text NOT NULL,
`benar` tinyint(1) not null default 0,
PRIMARY KEY (`id_jawaban`)
)
untuk meampilkan data jadi lebih gampang, data lebih konsisten.
session_start();
mysql_connect("localhost","user","pass");
mysql_select_db("nama_db");
//load jawaban secara acak
if(!isset($_SESSION['soal'])){
$query = mysql_query("select * from from tb_soal order by rand()");
while($soal = mysql_fetch_array($query)){
$_SESSION['soal'][] = $soal;
}
}
$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
// simpan jawaban user ke session selama masih ada soal
if($_POST){
$_SESSION['jawaban'][$_POST['id_soal']] = $_POST['jawab'];
}
if($page >= count($_SESSION['soal'])){
header("Location: hasil.php"); //tampilkan hasil
exit;
}
echo '<form method="post" action="?page='.($page+1).'">';
if(isset($_SESSION['soal'][$page])){
echo '<p>'.$_SESSION['soal'][$page]['soal'].'</p>';
$query = mysql_query("select * from tb_jawaban where '".$_SESSION['soal'][$page]['id_soal']."' order by rand()");
while($jawaban = mysql_fetch_array($query)){
echo '<input type="radio" name="jawab" value="'.$jawaban['id_jawaban'].'" id="jawab-'.$jawaban['id_jawaban'].'"/> '.$jawaban['jawaban'].'<br/>';
echo '<input name="id_soal" value="'.$_SESSION['soal'][$page]['id_soal'].'"/>';
}
}
echo '<input type="submit" value="Next"/>';
echo '</form>';
halaman tampil hasil.php
session_start();
mysql_connect("localhost","user","pass");
mysql_select_db("nama_db");
//cek kebenaran jawaban
$nilai = 0;
foreach($_SESSION['jawaban'] as $idSoal => $idJawaban){
$cek = mysql_query("select * from tb_jawaban where id_soal='".intval($idSoal)."' and id_jawaban='".intval($idJawaban)."' and benar='1'");
if(mysql_num_rows($cek) > 0){
$nilai += 1;
}
}
echo 'Hasil:'.$nilai;