top of page
  • fredorathanonin

PHP Code for Online Quiz with Timer: A Complete Guide to Building Your Own Quiz System



How to Create an Online Quiz with PHP and Timer




Online quizzes are a popular way of testing your knowledge, skills, or personality on various topics. They can be used for education, entertainment, or marketing purposes. Online quizzes usually consist of multiple-choice questions with different options, a timer that limits the time for each question or the whole quiz, and a score that shows how well you did.


In this tutorial, you will learn how to create your own online quiz system with PHP and MySQL. You will use PHP to write scripts that interact with the database and display the quiz pages. You will use MySQL to store the quiz data, such as questions, options, answers, and scores. You will use HTML and CSS to create and style the quiz pages. You will also learn how to use some basic features of PHP, such as classes, functions, sessions, forms, etc.




php code for online quiz with timer free download



By the end of this tutorial, you will be able to create an online quiz system that has the following features:


  • A database that stores all the quiz data



  • A config file that connects to the database



  • A class that contains methods for querying and manipulating the quiz data



  • A quiz page that displays one question at a time with four options



  • A timer that counts down from 10 seconds for each question



  • A result page that calculates and displays the score and feedback



  • An index page that displays the quiz instructions and starts the quiz



  • A style sheet that styles the quiz pages with CSS



Setting up the Database




The first step is to create a database and tables for storing the quiz data. You can use any database management tool, such as phpMyAdmin, to create and manage your database. In this tutorial, we will use phpMyAdmin as an example.


To create a database, follow these steps:


  • Open phpMyAdmin in your browser.



  • Click on New in the left sidebar.



  • Type db_quiz as the database name in the text box.



  • Click on Create.



To create tables, follow these steps:


php script for online quiz with timer and admin panel


php code for online exam system with timer and results


php code for online test with timer and multiple choice questions


php code for online quiz with timer and feedback


php code for online quiz with timer and video


php code for online quiz with timer and programming questions


php code for online quiz with timer and database


php code for online quiz with timer and score


php code for online quiz with timer and leaderboard


php code for online quiz with timer and instructions


php code for online quiz with timer and login


php code for online quiz with timer and question bank


php code for online quiz with timer and random questions


php code for online quiz with timer and edit option


php code for online quiz with timer and delete option


php code for online quiz with timer and pagination


php code for online quiz with timer and countdown


php code for online quiz with timer and resume option


php code for online quiz with timer and security


php code for online quiz with timer and validation


php code for online quiz with timer and certificate


php code for online quiz with timer and report


php code for online quiz with timer and analysis


php code for online quiz with timer and categories


php code for online quiz with timer and difficulty level


php code for online quiz with timer and hints


php code for online quiz with timer and images


php code for online quiz with timer and audio


php code for online quiz with timer and html5


php code for online quiz with timer and css3


php code for online quiz with timer and bootstrap


php code for online quiz with timer and jquery


php code for online quiz with timer and ajax


php code for online quiz with timer and json


php code for online quiz with timer and xml


php code for online quiz with timer and pdo


php code for online quiz with timer and mysqli


php code for online quiz with timer and oop


php code for online quiz with timer and mvc


php code for online quiz with timer and laravel


php code for online quiz with timer and wordpress


php code for online quiz with timer and drupal


php code for online quiz with timer and joomla


php code for online quiz with timer and moodle


php code for online quiz with timer and github


free download of php project on online quiz system with source code


  • Select db_quiz from the left sidebar.



  • Click on SQL in the top menu.



  • Type or paste the following SQL queries in the text box:



CREATE TABLE IF NOT EXISTS `questions` ( `qid` int (11) NOT NULL AUTO_INCREMENT, `question` varchar (150) NOT NULL, `is_enabled` int (11) NOT NULL, PRIMARY KEY (`qid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `options` ( `oid` int (11) NOT NULL AUTO_INCREMENT, `qid` int (11) NOT NULL, `option` varchar (100) NOT NULL, `is_correct` int (11) NOT NULL, PRIMARY KEY (`oid`), FOREIGN KEY (`qid`) REFERENCES `questions` (`qid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `scores` ( `sid` int (11) NOT NULL AUTO_INCREMENT, `score` int (11) NOT NULL, `date` date NOT NULL, PRIMARY KEY (`sid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


  • Click on Go to execute the queries and create the tables.



The questions table will store the quiz questions and a flag that indicates whether the question is enabled or not. The qid column is the primary key that uniquely identifies each question. The question column is the text of the question. The is_enabled column is a value of 1 or 0 that indicates whether the question is active or not.


The options table will store the quiz options and a flag that indicates whether the option is correct or not. The oid column is the primary key that uniquely identifies each option. The qid column is the foreign key that references the qid column in the questions table. The option column is the text of the option. The is_correct column is a value of 1 or 0 that indicates whether the option is the right answer or not.


The scores table will store the quiz scores and the date of taking the quiz. The sid column is the primary key that uniquely identifies each score. The score column is the number of correct answers out of 10. The date column is the date of taking the quiz.


Creating the PHP Scripts




The next step is to create some PHP scripts that will interact with the database and display the quiz pages. You will need to create four PHP files: config.php, quizclass.php, quiz.php, and result.php.


To create a config.php file, follow these steps:


  • Create a new file named config.php in your text editor.



  • Type or paste the following code in the file:



<?php // Define database credentials define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASS', ''); define('DB_NAME', 'db_quiz'); // Create a database connection $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); // Check for connection errors if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);


  • Save and close the file.



The config.php file will store the database credentials and create a database connection using the mysqli extension. You will need to change the values of DB_USER and DB_PASS according to your own database settings. The define function will create constants that can be used throughout the script. The new mysqli function will create an object that represents the connection to the database. The connect_error property will check for any connection errors and display them if any. To create a quiz.php file, follow these steps:


  • Create a new file named quiz.php in your text editor.



  • Type or paste the following code in the file:



<?php // Start a session session_start(); // Include the quiz class file require_once 'quizclass.php'; // Create a new quiz object $quiz = new Quiz(); // Check if the quiz has started or not if (!isset($_SESSION['start'])) // If not, redirect to the index page header('Location: index.php'); exit(); // Check if the quiz has ended or not if (isset($_SESSION['end'])) // If yes, redirect to the result page header('Location: result.php'); exit(); // Check if the question number is set or not if (!isset($_SESSION['qno'])) // If not, set it to 1 $_SESSION['qno'] = 1; // Check if the score is set or not if (!isset($_SESSION['score'])) // If not, set it to 0 $_SESSION['score'] = 0; // Get the question number from the session $qno = $_SESSION['qno']; // Get the questions array from the session $questions = $_SESSION['questions']; // Get the question id from the questions array based on the question number $qid = $questions[$qno - 1]['qid']; // Get the question text from the questions array based on the question number $question = $questions[$qno - 1]['question']; // Get the options array for the question id from the quiz object $options = $quiz->getOptions($qid); // Check if the user has submitted an answer or not if (isset($_POST['submit'])) // If yes, get the user answer from the post data $user_answer = $_POST['option']; // Get the correct answer for the question id from the quiz object $correct_answer = $quiz->getAnswer($qid); // Compare the user answer and the correct answer if ($user_answer == $correct_answer) // If they are equal, increment the score by 1 $_SESSION['score']++; // Increment the question number by 1 $_SESSION['qno']++; // Check if the question number is greater than 10 or not if ($_SESSION['qno'] > 10) // If yes, set the end flag to true $_SESSION['end'] = true; // Reload the page to display the next question or result header('Location: quiz.php'); exit();


  • Save and close the file.



The quiz.php file will display one question at a time with four options and a timer. It will also handle the user input and update the score and question number. The session_start function will start a session that will store some variables across different pages. The require_once function will include the quiz class file that contains the quiz object. The header function will redirect to another page based on some conditions. The isset function will check if a variable is set or not. The $_SESSION variable will access the session variables. The $_POST variable will access the post data from a form. The exit function will stop further execution of the script. To create a result.php file, follow these steps:


  • Create a new file named result.php in your text editor.



  • Type or paste the following code in the file:



<?php // Start a session session_start(); // Include the quiz class file require_once 'quizclass.php'; // Create a new quiz object $quiz = new Quiz(); // Check if the quiz has ended or not if (!isset($_SESSION['end'])) // If not, redirect to the quiz page header('Location: quiz.php'); exit(); // Get the score from the session $score = $_SESSION['score']; // Insert the score and date into the scores table using the quiz object $quiz->insertScore($score); // Destroy the session session_destroy();


  • Save and close the file.



The result.php file will calculate and display the score and feedback. It will also insert the score and date into the scores table and destroy the session. The session_destroy function will end the session and delete all the session variables.


Creating the HTML Pages




The next step is to create some HTML pages that will display the quiz instructions, questions, options, and results. You will need to create two HTML files: index.php and style.css.


To create an index.php file, follow these steps:


  • Create a new file named index.php in your text editor.



  • Type or paste the following code in the file:



<!DOCTYPE html>


<html lang="en">


<head>


<meta charset="UTF-8">


<meta name="viewport" content="width=device-width, initial-scale=1.0">


<title>Online Quiz with PHP and Timer</title>


<link rel="stylesheet" href="style.css">


</head>


<body>


<div >


<h1>Online Quiz with PHP and Timer</h1>


<p>Welcome to the online quiz system. This quiz consists of 10 multiple-choice questions on general knowledge. You will have 10 seconds to answer each question. Your score will be displayed at the end of the quiz.</p>


<p>To start the quiz, click on the button below.</p>


<form action="quiz.php" method="post">


<input type="submit" name="start" value="Start Quiz" >


</form>


</div>


</body>


</html>


  • Save and close the file.



The index.php file will display the quiz instructions and a button to start the quiz. It will use a form with a post method to send a start variable to the quiz.php file. It will also link to the style.css file for styling. To create a style.css file, follow these steps:


  • Create a new file named style.css in your text editor.



  • Type or paste the following code in the file:



* box-sizing: border-box; margin: 0; padding: 0; .container max-width: 800px; margin: 0 auto; padding: 20px; h1 text-align: center; font-size: 36px; color: #333; p font-size: 18px; color: #666; table width: 100%; border-collapse: collapse; th, td padding: 10px; border: 1px solid #ccc; th background-color: #eee; .button display: block; width: 200px; margin: 20px auto; padding: 10px; border: none; border-radius: 10px; background-color: #0099ff; color: #fff; font-size: 20px; cursor: pointer; .timer float: right; font-size: 24px; color: #f00;


  • Save and close the file.



The style.css file will style the quiz pages with CSS. It will use some basic properties, such as box-sizing, margin, padding, width, border, background-color, color, font-size, etc. It will also use some selectors, such as *, .container, h1, p, table, th, td, .button, .timer, etc.


Testing and Running the Quiz




The final step is to test and run the quiz on a local server or a web host. You will need to insert some sample data into the database tables for testing purposes. You will also need to run a local server or upload the files to a web host.


To insert some sample data into the database tables, follow these steps:


  • Open phpMyAdmin in your browser.



  • Select db_quiz from the left sidebar.



  • Click on SQL in the top menu.



  • Type or paste the following SQL queries in the text box:



INSERT INTO questions (question, is_enabled) VALUES ('What is the capital of France?', 1), ('Who wrote Hamlet?', 1), ('Which planet is the second from the Sun?', 1), ('What is the name of the largest bone in the human body?', 1), ('Which animal has the longest lifespan?', 1), ('What is the name of the phobia that means fear of spiders?', 1), ('Which country won the FIFA World Cup in 2018?', 1), ('What is the name of the currency used in Japan?', 1), ('Who painted the Mona Lisa?', 1), ('What is the name of the smallest country in the world?', 1); INSERT INTO options (qid, option, is_correct) VALUES (1, 'Paris', 1), (1, 'Berlin', 0), (1, 'Rome', 0), (1, 'London', 0), (2, 'William Shakespeare', 1), (2, 'Charles Dickens', 0), (2, 'Mark Twain', 0), (2, 'Leo Tolstoy', 0), (3, 'Mercury', 0), (3, 'Venus', 1), (3, 'Earth', 0), (3, 'Mars', 0), (4, 'Femur', 1), (4, 'Humerus', 0), (4, 'Tibia', 0), (4, 'Radius', 0), (5, 'Bowhead Whale', 1), (5, 'Giant Tortoise', 0), (5, 'Elephant', 0), (5, 'Parrot', 0), (6, 'Arachnophobia', 1), (6, 'Acrophobia', 0), (6, 'Claustrophobia', 0), (6, 'Agoraphobia', 0), (7, 'France', 1), (7, 'Croatia', 0), (7, 'Brazil', 0), (7, 'Germany', 0), (8, 'Yen', 1), (8, 'Won', 0), (8, 'Peso', 0), (8, 'Rupee', 0), (9, 'Leonardo da Vinci', 1), (9, 'Vincent van Gogh', 0), ( 9, 'Pablo Picasso', 0), (9, 'Michelangelo', 0), (10, 'Vatican City', 1), (10, 'Monaco', 0), (10, 'Nauru', 0), (10, 'Maldives', 0);


  • Click on Go to execute the queries and insert the data.



The SQL queries will insert some sample questions, options, and answers into the questions and options tables. You can change or add more data as you wish.


To run the quiz on a local server, follow these steps:


  • Make sure you have a local server installed on your computer, such as XAMPP, WAMP, or MAMP.



  • Copy all the files (config.php, quizclass.php, quiz.php, result.php, index.php, and style.css) into the htdocs folder of your local server.



  • Open your browser and type localhost/index.php in the address bar.



  • Follow the instructions and start the quiz.



To run the quiz on a web host, follow these steps:


  • Make sure you have a web hosting account that supports PHP and MySQL.



  • Create a database and tables on your web host using phpMyAdmin or any other tool.



  • Insert some sample data into the database tables using SQL queries or any other tool.



  • Change the database credentials in the config.php file according to your web host settings.



  • Upload all the files (config.php, quizclass.php, quiz.php, result.php, index.php, and style.css) to your web host using FTP or any other tool.



  • Open your browser and type your domain name or web address followed by /index.php in the address bar.



  • Follow the instructions and start the quiz.



Conclusion




In this tutorial, you have learned how to create an online quiz system with PHP and MySQL. You have used PHP to write scripts that interact with the database and display the quiz pages. You have used MySQL to store the quiz data, such as questions, options, answers, and scores. You have used HTML and CSS to create and style the quiz pages. You have also learned how to use some basic features of PHP, such as classes, functions, sessions, forms, etc.


You can improve or extend the quiz system by adding more features or functionalities. For example, you can:


  • Add more questions and options to the database.



  • Add more categories or topics to the quiz.



  • Add more types of questions, such as true/false, fill in the blanks, etc.



  • Add more feedback or explanations to the results.



  • Add more security or validation to the quiz.



  • Add more styles or effects to the quiz pages.



Here are some links and resources for further learning:




















Frequently Asked Questions





  • What is PHP?



PHP is a server-side scripting language that is widely used for web development. It can be embedded into HTML and executed on a web server. It can also interact with databases and perform various tasks.


  • What is MySQL?



MySQL is a relational database management system that is used to store and manipulate data. It can be accessed by various programming languages, such as PHP. It can also handle large amounts of data and perform complex queries.


  • What is HTML?



HTML is a markup language that is used to create web pages. It defines the structure and content of a web page using tags and attributes. It can also include other elements, such as images, links, forms, etc.


  • What is CSS?



CSS is a style sheet language that is used to style web pages. It CSS is a style sheet language that is used to style web pages. It can control the appearance and layout of web elements, such as fonts, colors, backgrounds, borders, etc. It can also create animations and transitions.


  • What is a session?



A session is a way of storing and transferring data across different web pages. It can be used to maintain the state and information of a user or a quiz. It can also be used to secure and validate the quiz.


  • What is a timer?



A timer is a feature that limits the time for each question or the whole quiz. It can create a sense of urgency and challenge for the user. It can also prevent cheating and improve the accuracy of the quiz. 44f88ac181


0 views0 comments

Recent Posts

See All
bottom of page