0% found this document useful (0 votes)
3 views

Deep

The document provides a comprehensive code solution for a web quiz application, including database configuration, quiz page, answer saving, submission, results display, and answer review functionalities. It features session-based answer storage, auto-saving, subject navigation, progress tracking, a timed quiz, and result calculation. Additionally, it emphasizes the importance of creating database tables, implementing error handling, sanitizing inputs, and ensuring security measures.

Uploaded by

Saifullahi Auwal
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Deep

The document provides a comprehensive code solution for a web quiz application, including database configuration, quiz page, answer saving, submission, results display, and answer review functionalities. It features session-based answer storage, auto-saving, subject navigation, progress tracking, a timed quiz, and result calculation. Additionally, it emphasizes the importance of creating database tables, implementing error handling, sanitizing inputs, and ensuring security measures.

Uploaded by

Saifullahi Auwal
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Here's a comprehensive code solution for your web quiz application.

Due to space
constraints, I'll break it into relevant sections:

1. Database Configuration (config.php):


```php
<?php
session_start();
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'quiz_db';

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Initialize session variables if not set


if (!isset($_SESSION['answers'])) {
$_SESSION['answers'] = array();
}
if (!isset($_SESSION['timer'])) {
$_SESSION['timer'] = 3600; // 1 hour timer
}
?>
```

2. Quiz Page (quiz.php):


```php
<?php include 'config.php';
// Fetch student details
$stmt = $conn->prepare("SELECT * FROM registrations WHERE reg_number = ?");
$stmt->bind_param("s", $_SESSION['regno']);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();

$subjects = ['english', $student['subject1'], $student['subject2'],


$student['subject3']];
$current_subject = isset($_GET['subject']) ? $_GET['subject'] : 'english';
$current_q = isset($_GET['q']) ? (int)$_GET['q'] : 1;

// Fetch questions for current subject


$limit = $current_subject === 'english' ? 60 : 40;
$stmt = $conn->prepare("SELECT * FROM questions WHERE subject = ? LIMIT ?");
$stmt->bind_param("si", $current_subject, $limit);
$stmt->execute();
$questions = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>Quiz</title>
<style>
/* Add your CSS styles here */
.subject-nav { display: flex; gap: 10px; }
.question-nav { display: flex; flex-wrap: wrap; gap: 5px; }
.answered { background: green; color: white; }
</style>
</head>
<body>
<div class="timer">Time Left: <span id="timer"><?= gmdate("H:i:s",
$_SESSION['timer']) ?></span></div>

<div class="subject-nav">
<?php foreach($subjects as $subj): ?>
<a href="?subject=<?= $subj ?>&q=1"><?= $subj ?></a>
<?php endforeach; ?>
</div>

<div class="progress">
<?php
$answered = count(array_filter($_SESSION['answers'][$current_subject] ??
[]));
echo "Progress: $answered/$limit";
?>
</div>

<form method="post" id="quizForm">


<?php $question = $questions[$current_q-1]; ?>
<div class="question"><?= $question['question'] ?></div>
<?php for($i=1; $i<=4; $i++): ?>
<label>
<input type="radio" name="answer" value="option<?= $i ?>"
<?= ($_SESSION['answers'][$current_subject][$current_q] ?? '')
=== "option$i" ? 'checked' : '' ?>>
<?= $question["option$i"] ?>
</label><br>
<?php endfor; ?>
</form>

<div class="question-nav">
<?php for($i=1; $i<=$limit; $i++): ?>
<a href="?subject=<?= $current_subject ?>&q=<?= $i ?>"
class="<?= isset($_SESSION['answers'][$current_subject][$i]) ?
'answered' : '' ?>">
<?= $i ?>
</a>
<?php endfor; ?>
</div>

<div class="navigation">
<?php if($current_q > 1): ?>
<a href="?subject=<?= $current_subject ?>&q=<?= $current_q-1 ?
>">Previous</a>
<?php endif; ?>

<?php if($current_q < $limit): ?>


<a href="?subject=<?= $current_subject ?>&q=<?= $current_q+1 ?
>">Next</a>
<?php else: ?>
<a href="submit.php">Submit Quiz</a>
<?php endif; ?>
</div>

<script>
// Timer
let timer = <?= $_SESSION['timer'] ?>;
setInterval(() => {
timer--;
document.getElementById('timer').textContent = new Date(timer * 1000)
.toISOString().substr(11, 8);
if(timer <= 0) window.location.href = 'submit.php';
}, 1000);

// Auto-save answers
document.querySelectorAll('input[type=radio]').forEach(input => {
input.addEventListener('change', () => {
fetch('save_answer.php', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
subject: '<?= $current_subject ?>',
question: <?= $current_q ?>,
answer: input.value
})
});
});
});
</script>
</body>
</html>
```

3. Save Answer (save_answer.php):


```php
<?php
include 'config.php';
$data = json_decode(file_get_contents('php://input'), true);
$_SESSION['answers'][$data['subject']][$data['question']] = $data['answer'];
?>
```

4. Submit and Results (submit.php):


```php
<?php include 'config.php';
// Calculate scores
$scores = [];
foreach($_SESSION['answers'] as $subject => $answers) {
$stmt = $conn->prepare("SELECT * FROM questions WHERE subject = ?");
$stmt->bind_param("s", $subject);
$stmt->execute();
$questions = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$score = 0;
foreach($answers as $q_num => $answer) {
if($questions[$q_num-1]['option1'] === $answer) $score++;
}
$scores[$subject] = $score;
}

// Clear session data


unset($_SESSION['answers']);
unset($_SESSION['timer']);
?>
<!DOCTYPE html>
<html>
<head>
<title>Results</title>
</head>
<body>
<h1>Your Scores:</h1>
<?php foreach($scores as $subject => $score): ?>
<div><?= ucfirst($subject) ?>: <?= $score ?></div>
<?php endforeach; ?>
<div>Total: <?= array_sum($scores) ?></div>

<h2>Review Answers:</h2>
<div class="subject-nav">
<?php foreach(array_keys($scores) as $subj): ?>
<a href="review.php?subject=<?= $subj ?>&q=1"><?= $subj ?></a>
<?php endforeach; ?>
</div>
</body>
</html>
```

5. Review Page (review.php):


```php
<?php include 'config.php';
$subject = $_GET['subject'];
$current_q = (int)$_GET['q'] ?? 1;

$stmt = $conn->prepare("SELECT * FROM questions WHERE subject = ?");


$stmt->bind_param("s", $subject);
$stmt->execute();
$questions = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>Review</title>
</head>
<body>
<div class="question-nav">
<?php for($i=1; $i<=count($questions); $i++): ?>
<a href="?subject=<?= $subject ?>&q=<?= $i ?>"><?= $i ?></a>
<?php endfor; ?>
</div>

<?php $question = $questions[$current_q-1]; ?>


<div class="question"><?= $question['question'] ?></div>
<?php for($i=1; $i<=4; $i++): ?>
<div <?= $question['option1'] === "option$i" ? 'style="color:green;"' :
'' ?>
<?= ($_SESSION['answers'][$subject][$current_q] ?? '') === "option$i" ?
'style="background:yellow;"' : '' ?>>
<?= $question["option$i"] ?>
</div>
<?php endfor; ?>
</body>
</html>
```

This implementation includes:


- Session-based answer storage
- Auto-saving answers
- Subject navigation
- Progress tracking
- Timed quiz
- Result calculation
- Answer review system
- Responsive navigation elements

Make sure to:


1. Create the database tables as specified
2. Add proper error handling
3. Sanitize all user inputs
4. Add proper security measures (SQL injection prevention, XSS protection)
5. Add proper styling
6. Test thoroughly with different user scenarios

You might also like