<?php
session_start();
mysql_connect("localhost","root","blah");
mysql_select_db("test");
//load jawaban secara acak
if(!isset($_SESSION['soal'])){
$query = mysql_query("select * 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 id_soal='".$_SESSION['soal'][$page]['id_soal']."' order by rand()");
echo '<input name="id_soal" type="hidden" value="'.$_SESSION['soal'][$page]['id_soal'].'"/>';
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 type="submit" value="Next"/>';
echo '</form>';
file hasil.php<?php
session_start();
mysql_connect("localhost","root","blah");
mysql_select_db("test");
//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;
$_SESSION['jawaban'] = array();
unset($_SESSION['soal']);
<?php
session_start();
mysql_connect("localhost","root","blah");
mysql_select_db("test");
//load jawaban secara acak
if(!isset($_SESSION['soal'])){
$query = mysql_query("select * from tb_soal order by rand()");
while($soal = mysql_fetch_array($query)){
$_SESSION['soal'][] = $soal;
}
}
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
$start = $page * $limit;
// simpan jawaban user ke session selama masih ada soal
if($_POST){
foreach($_POST['id_soal'] as $idSoal){
$_SESSION['jawaban'][$idSoal] = $_POST['jawab'][$idSoal];
}
}
if($start >= count($_SESSION['soal'])){
header("Location: hasil.php"); //tampilkan hasil
exit;
}
echo '<form method="post" action="?page='.($page+1).'">';
for($i = $start; $i < $start + $limit; $i++){
if(isset($_SESSION['soal'][$i])){
echo '<p>'.$_SESSION['soal'][$i]['soal'].'</p>';
$query = mysql_query("select * from tb_jawaban where id_soal='".$_SESSION['soal'][$i]['id_soal']."' order by rand()");
echo '<input name="id_soal[]" type="hidden" value="'.$_SESSION['soal'][$i]['id_soal'].'"/>';
while($jawaban = mysql_fetch_array($query)){
echo '<input type="radio" name="jawab['.$_SESSION['soal'][$i]['id_soal'].']" value="'.$jawaban['id_jawaban'].'" id="jawab-'.$jawaban['id_jawaban'].'"/> '.$jawaban['jawaban'].'<br/>';
}
}
}
echo '<input type="submit" value="Next"/>';
echo '</form>';
kurang lebih seperti itu, pengecekan nilai sama seperti yang sudah dijelaskan sebelumnya.