Simple True or False Quiz Generator

Getting Started

I've used CDN for Bootstrap so you need internet connection for it to work.

Creating our Database

First, we are going to create our MySQL database and insert sample questions with certain answer. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as quiz. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `truefalse` (
  2. `questionid` int(11) NOT NULL AUTO_INCREMENT,
  3. `question` text NOT NULL,
  4. `answer` int(1) NOT NULL,
  5. PRIMARY KEY(`questionid`)
  1. INSERT INTO `truefalse` (`questionid`, `question`, `answer`) VALUES
  2. (1, 'Parent constructors are not called implicitly if the child class defines a constructor.', 1),
  3. (2, 'Interface constant can be override in class implementing the interface.', 0),
  4. (3, 'Static methods can be call with class name and colon operator, $this is not available inside the method declared as static.', 1),
  5. (4, 'Static properties can be accessed through the object using the arrow operator ->.', 0),
  6. (5, 'If parent class has Final method abc(). Method abc() can be overridden in child class.', 0),
  7. (6, 'In PHP, a class can be inherited from one base class and with multiple base classes.', 0),
  8. (7, 'To create instance of class \"new\" keyword is not required.', 0),
  9. (8, '$this is a reference to the calling object', 1),
  10. (9, 'The variable name is case-sensitive in PHP.', 1),
  11. (10, 'PHP is an open source software', 1);
database mysql

Creating our Questions

Next, we're going to create our index which contains our questions that we fetch from database. We are going to name it as index.php.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Simple True or False Quiz Generator</title>
  6. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">Simple True or False Quiz Generator</h1>
  11. <div class="row">
  12. <div class="col-md-6 col-md-offset-3">
  13. <form method="POST" action="check_answer.php">
  14. <?php
  15. $iterate = 1;
  16. $conn = new mysqli('localhost', 'root', '', 'quiz');
  17.  
  18. //this will arrange the questions randomly and 10 only
  19. $sql = "SELECT * FROM truefalse ORDER BY rand() LIMIT 10";
  20. $query = $conn->query($sql);
  21. while($row = $query->fetch_array()){
  22. ?>
  23. <div>
  24. <input type="hidden" value="<?php echo $row['questionid']; ?>||<?php echo $iterate; ?>" name="questionid[]">
  25. <p><?php echo $iterate; ?>. <?php echo $row['question']; ?></p>
  26. <input type="radio" name="answer_<?php echo $iterate; ?>" value="1"> True
  27. <input type="radio" name="answer_<?php echo $iterate; ?>" value="0"> False
  28. </div><br>
  29. <?php
  30.  
  31. $iterate++;
  32. }
  33.  
  34. ?>
  35. <button type="submit" class="btn btn-primary">Save</button>
  36. <br><br>
  37. </form>
  38. </div>
  39. </div>
  40. </div>
  41. </body>
  42. </html>

Creating the Answers and Score

Lastly, we create the page where we can view the answer and the score. We are going to name this as check_answer.php.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Simple True or False Quiz Generator</title>
  6. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">Simple True or False Quiz Generator</h1>
  11. <div class="row">
  12. <div class="col-md-6 col-md-offset-3">
  13. <?php
  14. $conn = new mysqli('localhost', 'root', '', 'quiz');
  15.  
  16. $score = 0;
  17.  
  18. foreach($_POST['questionid'] as $question):
  19. $info = explode("||", $question);
  20.  
  21. $questionid = $info[0];
  22. $iterate = $info[1];
  23.  
  24. $sql = "SELECT * FROM truefalse WHERE questionid = '$questionid'";
  25. $query = $conn->query($sql);
  26. $row = $query->fetch_array();
  27.  
  28. ?>
  29. <div>
  30. <p><?php echo $iterate; ?>. <?php echo $row['question']; ?></p>
  31. <p>Correct Answer: <?php if($row['answer']==1){ echo 'True';} else{ echo 'False';} ?></p>
  32. <?php
  33. if (isset($_POST['answer_'.$iterate])){
  34. ?>
  35. You Answered: <?php if($_POST['answer_'.$iterate] == 1){echo 'True';} else{echo 'False';} ?><br>
  36. <?php
  37. if ($_POST['answer_'.$iterate] == $row['answer']){
  38. echo '<span class="glyphicon glyphicon-check"></span> Correct<br><br>';
  39. $score = $score + 1;
  40. }
  41. else{
  42. echo '<span class="glyphicon glyphicon-remove"></span> Wrong<br><br>';
  43. }
  44. }
  45. ?>
  46. </div>
  47. <?php
  48.  
  49. endforeach;
  50.  
  51. ?>
  52. <h2>Score: <?php echo $score; ?></h2>
  53. </div>
  54. </div>
  55. </div>
  56. </body>
  57. </html>
That ends this tutorial. Happy Coding :)

Comments

Submitted byAnonymous (not verified)on Thu, 05/05/2022 - 00:00

True or false. Ashoka allowed religious freedom to all people in his empire.

Add new comment