0% found this document useful (0 votes)
4 views5 pages

QuizAPP

The document is a PHP script for a quiz website that presents multiple-choice questions to users. It manages user sessions to track the current question, score, and answers, allowing users to submit their answers and see their results upon completion. The quiz includes questions about geography, science, mathematics, and literature.

Uploaded by

Subrata Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

QuizAPP

The document is a PHP script for a quiz website that presents multiple-choice questions to users. It manages user sessions to track the current question, score, and answers, allowing users to submit their answers and see their results upon completion. The quiz includes questions about geography, science, mathematics, and literature.

Uploaded by

Subrata Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

QUIZ Website

<?php
session_start();

$questions = [
1 => [
'question' => 'What is the capital of France?',
'options' => [
'A' => 'London',
'B' => 'Berlin',
'C' => 'Paris',
'D' => 'Madrid'
],
'correct' => 'C'
],
2 => [
'question' => 'Which planet is known as the Red Planet?',
'options' => [
'A' => 'Venus',
'B' => 'Mars',
'C' => 'Jupiter',
'D' => 'Saturn'
],
'correct' => 'B'
],
3 => [
'question' => 'What is 2 + 2?',
'options' => [
'A' => '3',
'B' => '4',
'C' => '5',
'D' => '22'
],
'correct' => 'B'
],
4 => [
'question' => 'Who wrote "Romeo and Juliet"?',
'options' => [
'A' => 'Charles Dickens',
'B' => 'Jane Austen',
'C' => 'William Shakespeare',
'D' => 'Mark Twain'
],
'correct' => 'C'
],
5 => [
'question' => 'Which element has the chemical symbol "O"?',
'options' => [
'A' => 'Gold',
'B' => 'Oxygen',
'C' => 'Osmium',
'D' => 'Oganesson'
],
'correct' => 'B'
]
];

// Initialize or reset quiz


if (!isset($_SESSION['current_question']) || isset($_POST['restart'])) {
$_SESSION['current_question'] = 1;
$_SESSION['score'] = 0;
$_SESSION['answers'] = [];
}

// Process submitted answer


if (isset($_POST['submit']) && isset($_POST['answer'])) {
$current = $_SESSION['current_question'];
$selected_answer = $_POST['answer'];

// Store the answer


$_SESSION['answers'][$current] = $selected_answer;

// Check if correct
if ($selected_answer == $questions[$current]['correct']) {
$_SESSION['score']++;
}

// Move to next question


$_SESSION['current_question']++;
}

// Determine if the quiz is complete


$quiz_complete = $_SESSION['current_question'] > count($questions);

// Get current question if quiz is not complete


if (!$quiz_complete) {
$current = $_SESSION['current_question'];
$question = $questions[$current];
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Quiz App</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.quiz-container {
background-color: #f5f5f5;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.question {
font-size: 18px;
margin-bottom: 20px;
}
.options {
margin-bottom: 20px;
}
.option {
margin-bottom: 10px;
}
.result {
margin-top: 20px;
font-weight: bold;
}
.correct {
color: green;
}
.incorrect {
color: red;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="quiz-container">
<h1>PHP Quiz App</h1>

<?php if (!$quiz_complete): ?>


<form method="post" action="">
<div class="question">
<h3>Question <?php echo $current; ?> of <?php echo
count($questions); ?></h3>
<p><?php echo $question['question']; ?></p>
</div>

<div class="options">
<?php foreach ($question['options'] as $key => $option): ?>
<div class="option">
<input type="radio" id="option<?php echo $key; ?>"
name="answer" value="<?php echo $key; ?>" required>
<label for="option<?php echo $key; ?>"><?php echo $key; ?>.
<?php echo $option; ?></label>
</div>
<?php endforeach; ?>
</div>

<button type="submit" name="submit">Submit Answer</button>


</form>
<?php else: ?>
<h2>Quiz Complete!</h2>
<p>Your score: <?php echo $_SESSION['score']; ?> out of <?php echo
count($questions); ?></p>

<h3>Your Answers:</h3>
<?php foreach ($_SESSION['answers'] as $q_num => $answer): ?>
<div class="<?php echo ($answer == $questions[$q_num]['correct']) ?
'correct' : 'incorrect'; ?>">
<p>
Question <?php echo $q_num; ?>:
You selected <?php echo $answer; ?> (<?php echo
$questions[$q_num]['options'][$answer]; ?>).
<?php if ($answer != $questions[$q_num]['correct']): ?>
The correct answer was <?php echo $questions[$q_num]['correct'];
?>
(<?php echo $questions[$q_num]['options'][$questions[$q_num]
['correct']]; ?>).
<?php endif; ?>
</p>
</div>
<?php endforeach; ?>

<form method="post" action="">


<button type="submit" name="restart">Restart Quiz</button>
</form>
<?php endif; ?>
</div>
</body>
</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy