Skip to content

Instantly share code, notes, and snippets.

@cahsowan
Created March 16, 2018 02:40
Show Gist options
  • Save cahsowan/41b5359db2302653bbaa980db99ed406 to your computer and use it in GitHub Desktop.
Save cahsowan/41b5359db2302653bbaa980db99ed406 to your computer and use it in GitHub Desktop.
Contoh sederhana ujian pilihan ganda dengan PHP
<?php
$soal = [
1 => [
'konten' => 'Siapakah sahabat yang menemani Nabi hijrah ke madinah?',
'pilihan' => [
'A' => 'Umar',
'B' => 'Usman',
'C' => 'Ali',
'D' => 'Abu Bakar',
]
],
2 => [
'konten' => 'Siapakah sahabat yang bergelar al Faruq?',
'pilihan' => [
'A' => 'Umar',
'B' => 'Usman',
'C' => 'Ali',
'D' => 'Abu Bakar',
]
],
];
$kunci_jawaban = [
1 => 'D',
2 => 'A',
];
$benar = [];
$nilai = 0;
if ($_POST) {
$jawabanmu = $_POST['jawabanmu'];
if (count($jawabanmu) < count($kunci_jawaban)) {
die('harap lengkapi jawaban!');
}
$benar = [];
foreach ($jawabanmu as $nomor => $huruf) {
if ($kunci_jawaban[$nomor] == $huruf) {
array_push($benar, $nomor);
}
}
$nilai = (100 / count($kunci_jawaban) * count($benar));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ujian</title>
</head>
<body>
<h1>Ujian</h1>
<form action="/" method="POST">
<?php foreach ($soal as $nomor => $detail) : ?>
<div>
<p><?php echo $nomor . ' - ' . $detail['konten'] ?></p>
<ul>
<?php foreach ($detail['pilihan'] as $huruf => $isi) : ?>
<li>
<input type="radio" name="jawabanmu[<?php echo $nomor ?>]" value="<?php echo $huruf ?>">
<?php echo $huruf . ' - ' . $isi ?>
</li>
<?php endforeach ?>
</ul>
</div>
<?php endforeach ?>
<p>
<button>Kirim Jawaban</button>
</p>
</form>
<?php if ($_POST) : ?>
<table border="1">
<tr>
<th>Nomor</th>
<th>Jawaban Kamu</th>
<th>Kunci</th>
<th>Status</th>
</tr>
<?php foreach ($kunci_jawaban as $nomor => $huruf) : ?>
<tr>
<td><?php echo $nomor ?></td>
<td><?php echo $jawabanmu[$nomor] ?></td>
<td><?php echo $huruf ?></td>
<td><?php echo $huruf == $jawabanmu[$nomor] ? 'Benar' : 'Salah' ?></td>
</tr>
<?php endforeach ?>
</table>
<p>Nilai: <?php echo $nilai ?></p>
<?php endif ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment