Show sourcecode
The following files exists in this folder. Click to view.
account.php
admin_register.php
create_quiz.php
create_tables.php
index.php
login.php
nav.css
nav.php
quiz.php
quiz_select.php
register.php
result_details.php
result_simple.php
submit_create_quiz.php
submit_quiz.php
quiz.php
92 lines UTF-8 Windows (CRLF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
session_start();
// Kontrollera om användaren är inloggad
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
// Debugging:
// if (!isset($_GET['test_id']) || empty($_GET['test_id'])) {
// echo "Test-ID saknas.";
// exit;
// }
$test_id = $_GET['test_id'];
include ('../../incl/dbconnect.php');
try {
// Hämta testinformation
$stmt = $dbconn->prepare("SELECT * FROM testinfo_quiz WHERE id = ?");
$stmt->execute([$test_id]);
$test = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$test) {
echo "Testet hittades inte.";
exit;
}
// Hämta alla frågor för testet
$stmt = $dbconn->prepare("SELECT * FROM questions_quiz WHERE test_id = ?");
$stmt->execute([$test_id]);
$questions = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo "Fel vid hämtning av testdata: " . $e->getMessage();
exit;
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title><?php echo htmlspecialchars($test['titel']); ?></title>
<link rel="stylesheet" href="nav.css">
</head>
<body>
<?php include ('nav.php'); ?>
<h2><?php echo htmlspecialchars($test['titel']); ?></h2>
<?php if (!empty($test['beskrivning'])): ?>
<p><?php echo htmlspecialchars($test['beskrivning']); ?></p>
<?php endif; ?>
<form method="post" action="submit_quiz.php">
<?php if (!empty($questions)): ?>
<?php foreach($questions as $index => $question): ?>
<div>
<p><strong>Fråga <?php echo $index + 1; ?>:</strong> <?php echo htmlspecialchars($question['fråga_text']); ?></p>
<?php
// Hämta svarsalternativ för den aktuella frågan
$stmt = $dbconn->prepare("SELECT * FROM answers_quiz WHERE fråga_id = ?");
$stmt->execute([$question['id']]);
$answers = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<?php if (!empty($answers)): ?>
<?php foreach ($answers as $answer): ?>
<div>
<label>
<input type="radio" name="answer[<?php echo $question['id']; ?>]" value="<?php echo $answer['id']; ?>" required>
<?php echo htmlspecialchars($answer['svar_text']); ?>
</label>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>Inga svarsalternativ hittades för denna fråga.</p>
<?php endif; ?>
</div>
<hr>
<?php endforeach; ?>
<input type="hidden" name="test_id" value="<?php echo htmlspecialchars($test_id); ?>">
<input type="submit" value="Skicka in svar">
<?php else: ?>
<p>Inga frågor hittades för detta test.</p>
<?php endif; ?>
</form>
</body>
</html>