In: Computer Science
write a PHP program that initially has button called START QUIZ, once the user clicks on it, it should randomly select 5 questions from the array and display them in proper quiz format. the user should see a message as follows: START TIME"9:07 AM DATE: 8 OCT 2020 YOU HAVE ONLY 5 MIN TO SUMBIT YOUR QUIZ the user should submit the quiz within 5 minutes, so the quiz score should be shown out of 5. in addition it should shown a report specifying user answers in green if it's correct or red if it's incorrect . in case, the user submitted the form late, then the following msg should appear: SORRY, THE TOME IS OUT we might need to use PHP functions but we are not allowed to use COOKIES or SESSION
*here is what i've done so far, i don't know how to do the rest*
<?php
$questions[] ['q123'] = array (
'question'=>'PHP Stands for?',
'choice1'=>'PHP Hypertext Processor',
'choice2'=>'PHP Hyper Markup Processor',
'choice3'=>' PHP Hypertext Preprocessor',
'choice4'=>'PHP Hypertext Preprocessor',
$questions[] ['q124'] = array (
'question'=>'PHP is an example of ___________ scripting
language.',
'choice'=>'Server-side',
'choice2'=>'Client-side',
'choice3'=>'Browser-side',
'choice4'=>'In-side',
'correct'=>'Server-side' );
$questions[] ['q125'] = array (
'question'=>'PHP scripts are enclosed within _______',
'choice1'=>'<php> , </php>',
'choice2'=>'<?php , ?>',
'choice3'=>'?php , ?php',
'choice4'=>'None Of The Above',
'correct'=>'<?php , ?>' );
$questions[] ['q126'] = array (
'question'=>'Localhost IP is ...',
'choice'=>'192.168.0.1',
'choice2'=>'127.0.0.0',
'choice3'=>'1080:80',
'choice4'=>'Any Other',
'correct'=>'127.0.0.0' );
$questions[] ['q127'] = array (
'question'=>'Which of the following variables is not a
predefined variable?
',
'choice'=>'$get',
'choice2'=>'$request',
'choice3'=>'$post',
'choice4'=>'$ask',
'correct'=>'$ask' );
$questions[] ['q128'] = array (
'question'=>'Which of the following method sends input to a
script via a URL?',
'choice'=>'Get',
'choice2'=>'Post',
'choice3'=>'Both',
'choice4'=>'None',
'correct'=>'Get' );
$questions[] ['q129'] = array (
'question'=>'Which of the following is used to add multiple lone
comments in PHP?',
'choice'=>'//',
'choice2'=>'/* */',
'choice3'=>'{{}}',
'choice4'=>'{/\}',
'correct'=>'/* */' );
$questions[] ['q130'] = array (
'question'=>'Which sign is used to access variable of varabiles
in PHP?',
'choice'=>'You can't use 'macro parameter character #' in math
mode' );
$questions[] ['q131'] = array (
'question'=>'______ is used to unset a variable in PHP?',
'choice'=>'delete()',
'choice2'=>'unset()',
'choice3'=>'unlink()',
'choice4'=>'delete();',
'correct'=>'unset()' );
$questions[] ['q132'] = array (
'question'=>'Which function in PHP is used to get the length of
string variable?',
'choice'=>'count()',
'choice2'=>'strcount',
'choice3'=>'strlen',
'choice4'=>'len',
'correct'=>'strlen' );
$questions[] ['q133'] = array (
'question'=>'Which of the below symbols is a newlinw
character?',
'choice'=>'\n',
'choice2'=>'\r',
'choice3'=>'/n',
'choice4'=>'/r',
'correct'=>'/n' );
$questions[] ['q134'] = array (
'question'=>'Which of the sunctions is used to sort an array in
descending order?',
'choice'=>'asort()',
'choice2'=>'sort()',
'choice3'=>'rsort()',
'choice4'=>'dsort()',
'correct'=>'rsort()' );
?>
This is the html file where i have kept a button
<!DOCTYPE html>
<html>
<head>
<title>Quiz</title>
</head>
<body>
<form name="form" action="question.php">
<button>Start
Quiz</button>
</form>
</body>
</html>
Onclicking button it is redirected to the question.php file which is as under.
<?php
echo '<script type="text/javascript">
var myDate = new Date();
var date = myDate.getDate();
var month = myDate.getMonth();
var year = myDate.getFullYear();
var monthNames = ["Jan", "Feb", "March", "April", "May", "June",
"July", "Aug", "Sept", "Oct", "Nov", "Dec"
];
var hours = myDate.getHours();
var ampm = hours >= 12 ? \'PM\' : \'AM\';
hours = hours % 12;
hours = hours ? hours : 12;
var minutes = myDate.getMinutes();
minutes = minutes < 10 ? \'0\' + minutes : minutes;
var myTime = hours + " " + ampm + " : " + minutes +
" : " + myDate.getMilliseconds();
document.write("\nSTART TIME "+ myTime + " DATE: " + date + " " + monthNames[month] + " " + year + " YOU HAVE ONLY 5 MIN TO SUMBIT YOUR QUIZ");
function CountDown(duration, display) {
if (!isNaN(duration)) {
var timer = duration, minutes, seconds;
var interVal= setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
$(display).html("<b>" + minutes + "m : " + seconds + "s" + "</b>");
if (--timer < 0) {
timer = duration;
SubmitFunction();
$(\'#display\').empty();
clearInterval(interVal)
document.write("SORRY, THE TIME IS OUT");
}
},1000);
}
}
function SubmitFunction(){
$(\'form\').submit();
}
CountDown(300,$(\'#display\'));
</script>';
echo '<div id="display">';
echo ' ';
echo '</div>';
$Questions = array(
1 => array(
'Question' => 'PHP Stands for?',
'Answers' => array(
'A' => 'PHP Hypertext Processor',
'B' => 'PHP Hyper Markup Processor',
'C' => 'PHP Hypertext Preprocessor',
'D' => 'PHP Hypertext Preprocess'
),
'CorrectAnswer' => 'C'
),
2 => array(
'Question' => 'PHP is an example of ___________ scripting language.',
'Answers' => array(
'A' => 'Server-side',
'B' => 'Client-side',
'C' => 'In-side',
'D' => 'Brower-side'
),
'CorrectAnswer' => 'A'
),
3 => array(
'Question' => 'PHP scripts are enclosed within _______',
'Answers' => array(
'A' => '<php> , </php>',
'B' => '<?php , ?>',
'C' => '?php , ?php',
'D' => 'None Of The Above'
),
'CorrectAnswer' => 'B'
),
4 => array(
'Question' => 'Localhost IP is ..',
'Answers' => array(
'A' => '192.168.0.1',
'B' => '127.0.0.0',
'C' => '1080:80',
'D' => 'Any Other'
),
'CorrectAnswer' => 'B'
),
5 => array(
'Question' => 'Which of the following variables is not a predefined variable?',
'Answers' => array(
'A' => '$get',
'B' => '$post',
'C' => '$ask',
'D' => '$request'
),
'CorrectAnswer' => 'C'
),
6 => array(
'Question' => 'Which of the following method sends input to a script via a URL?',
'Answers' => array(
'A' => 'Get',
'B' => 'Post',
'C' => 'Both',
'D' => 'None'
),
'CorrectAnswer' => 'C'
),
7 => array(
'Question' => 'Which of the following is used to add multiple lone comments in PHP?',
'Answers' => array(
'A' => '//',
'B' => '/* */',
'C' => '{{}}',
'D' => '{/\}'
),
'CorrectAnswer' => 'B'
),
8 => array(
'Question' => 'Which sign is used to access variable of varabiles in PHP?',
'Answers' => array(
'A' => 'You can not use macro parameter character # in math mode'
),
'CorrectAnswer' => ''
),
9 => array(
'Question' => '______ is used to unset a variable in PHP?',
'Answers' => array(
'A' => 'delete()',
'B' => 'unset()',
'C' => 'unlink()',
'D' => 'set()'
),
'CorrectAnswer' => 'B'
),
10 => array(
'Question' => 'Which function in PHP is used to get the length of string variable?',
'Answers' => array(
'A' => 'count()',
'B' => 'strcount',
'C' => 'strlen',
'D' => 'len'
),
'CorrectAnswer' => 'C'
),
11 => array(
'Question' => 'Which of the below symbols is a newline character?',
'Answers' => array(
'A' => '\n',
'B' => '\r',
'C' => '/n',
'D' => '/r'
),
'CorrectAnswer' => 'C'
),
12 => array(
'Question' => 'Which of the functions is used to sort an array in descending order?',
'Answers' => array(
'A' => 'asort()',
'B' => 'sort()',
'C' => 'rsort()',
'D' => 'dsort()'
),
'CorrectAnswer' => 'C'
),
);
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
} else {
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; $counter++;
}
echo '<br /><hr>';
if ($counter=="")
{
$counter='0';
$results = "Your score: $counter/12";
}
else
{
$results = "Your score: $counter/12";
}
} echo $results;
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
<?php
$totalQuestionCount = count($Questions);
$randQuestions = array_chunk($Questions,5);
shuffle($Questions);
foreach ($Questions as $QuestionNo => $Value) {
?>
<h3><?php echo $Value['Question']; ?></h3>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
</div>
<?php }
if ($QuestionNo == $randQuestions) {
break;
}
?>
<?php } ?>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>