87 lines
2.0 KiB
PHP
87 lines
2.0 KiB
PHP
<?php
|
|
|
|
session_start();
|
|
|
|
define('QUESTIONS_FILE', 'questions.json');
|
|
|
|
$questions = json_decode(file_get_contents(QUESTIONS_FILE), true);
|
|
|
|
$totalQuestions = count($questions);
|
|
|
|
if (!isset($_SESSION['current_question'])) {
|
|
$_SESSION['current_question'] = 0;
|
|
$_SESSION['score'] = 0;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['answer'])) {
|
|
$current = $_SESSION['current_question'];
|
|
$selected = (int)$_POST['answer'];
|
|
|
|
if ($selected === $questions[$current]['answer']) {
|
|
$_SESSION['score']++;
|
|
}
|
|
|
|
$_SESSION['current_question']++;
|
|
|
|
if ($_SESSION['current_question'] >= $totalQuestions) {
|
|
$finished = true;
|
|
} else {
|
|
$finished = false;
|
|
}
|
|
} elseif (isset($_POST['reset'])) {
|
|
session_destroy();
|
|
header("Location: " . $_SERVER['PHP_SELF']);
|
|
exit;
|
|
} else {
|
|
$finished = false;
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<title>Quiz en ligne</title>
|
|
<link rel="stylesheet" href="style.css" />
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>Quiz en ligne basique</h1>
|
|
|
|
<?php if ($finished): ?>
|
|
|
|
<h2>Résultat final</h2>
|
|
<p>Votre score : <?= $_SESSION['score'] ?>/<?= $totalQuestions ?></p>
|
|
|
|
<form method="post" action="">
|
|
<button type="submit" name="reset">Recommencer le quiz</button>
|
|
</form>
|
|
|
|
<?php else: ?>
|
|
|
|
<?php
|
|
$current = $_SESSION['current_question'];
|
|
$q = $questions[$current];
|
|
?>
|
|
|
|
<form method="post" action="">
|
|
<p class="question"><?= htmlspecialchars($q['question']) ?></p>
|
|
<?php foreach ($q['choices'] as $index => $choice): ?>
|
|
<div class="choice">
|
|
<label>
|
|
<input type="radio" name="answer" value="<?= $index ?>" required />
|
|
<?= htmlspecialchars($choice) ?>
|
|
</label>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<button type="submit">Valider</button>
|
|
</form>
|
|
|
|
<p>Question <?= $current + 1 ?> sur <?= $totalQuestions ?></p>
|
|
|
|
<?php endif; ?>
|
|
|
|
</body>
|
|
</html>
|