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, 'Parent constructors are not called implicitly if the child class defines a constructor.', 1),
(2, 'Interface constant can be override in class implementing the interface.', 0),
(3, 'Static methods can be call with class name and colon operator, $this is not available inside the method declared as static.', 1),
(4, 'Static properties can be accessed through the object using the arrow operator ->.', 0),
(5, 'If parent class has Final method abc(). Method abc() can be overridden in child class.', 0),
(6, 'In PHP, a class can be inherited from one base class and with multiple base classes.', 0),
(7, 'To create instance of class \"new\" keyword is not required.', 0),
(8, '$this is a reference to the calling object', 1),
(9, 'The variable name is case-sensitive in PHP.', 1),
(10, 'PHP is an open source software', 1);
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.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple True or False Quiz Generator</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="page-header text-center">Simple True or False Quiz Generator</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form method="POST" action="check_answer.php">
<?php
$iterate = 1;
$conn = new mysqli('localhost', 'root', '', 'quiz');
//this will arrange the questions randomly and 10 only
$sql = "SELECT * FROM truefalse ORDER BY rand() LIMIT 10";
$query = $conn->query($sql);
while($row = $query->fetch_array()){
?>
<div>
<input type="hidden" value="<?php echo $row['questionid']; ?>||<?php echo $iterate; ?>" name="questionid[]">
<p><?php echo $iterate; ?>. <?php echo $row['question']; ?></p>
<input type="radio" name="answer_<?php echo $iterate; ?>" value="1"> True
<input type="radio" name="answer_<?php echo $iterate; ?>" value="0"> False
</div><br>
<?php
$iterate++;
}
?>
<button type="submit" class="btn btn-primary">Save</button>
<br><br>
</form>
</div>
</div>
</div>
</body>
</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.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple True or False Quiz Generator</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="page-header text-center">Simple True or False Quiz Generator</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<?php
$conn = new mysqli('localhost', 'root', '', 'quiz');
$score = 0;
foreach($_POST['questionid'] as $question):
$questionid = $info[0];
$iterate = $info[1];
$sql = "SELECT * FROM truefalse WHERE questionid = '$questionid'";
$query = $conn->query($sql);
$row = $query->fetch_array();
?>
<div>
<p><?php echo $iterate; ?>. <?php echo $row['question']; ?></p>
<p>Correct Answer: <?php if($row['answer']==1){ echo 'True';} else{ echo 'False';} ?></p>
<?php
if (isset($_POST['answer_'.$iterate])){
?>
You Answered: <?php if($_POST['answer_'.$iterate] == 1){echo 'True';} else{echo 'False';} ?><br>
<?php
if ($_POST['answer_'.$iterate] == $row['answer']){
echo '<span class="glyphicon glyphicon-check"></span> Correct<br><br>';
$score = $score + 1;
}
else{
echo '<span class="glyphicon glyphicon-remove"></span> Wrong<br><br>';
}
}
?>
</div>
<?php
endforeach;
?>
<h2>Score: <?php echo $score; ?></h2>
</div>
</div>
</div>
</body>
</html>
That ends this tutorial. Happy Coding :)