Question

In: Computer Science

write a PHP program that initially has button called START QUIZ, once the user clicks on...

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()' );


?>

Solutions

Expert Solution

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 
}
?>

Related Solutions

Provide two text boxes for the user to enter in 2 values. Once the user clicks...
Provide two text boxes for the user to enter in 2 values. Once the user clicks off the second box, the sum of the two numbers displayed as a header level 2. here is what i have , and i cant find a way to display the sum of the numbers in each box in the header along with the other text. <h1>Problem 6</h1> <input type="text" id="txt1" onkeyup="sum();" /> <input type="text" id="txt2" onkeyup="sum();" /> <h2 id="txt3" result=""> The sum of...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Assignment 2: Create a form that collects 5 different pieces of data. Once the user clicks...
Assignment 2: Create a form that collects 5 different pieces of data. Once the user clicks the submit button, the processing PHP file displays the data that was entered thru the form, performs a calculation, and displays the result. To receive full credit, students need to implement the following: Create variables with valid names and assign values to them Use literals and concatenate strings Use $_POST Use echo statements to display data on a page Code string and numeric expressions...
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Write a program called whilebun.py, in which the user is asked to guess the name of...
Write a program called whilebun.py, in which the user is asked to guess the name of your favorite DBVac vacuum cleaner model. Your program should: · Include a comment in the first line with your name. · Include comments describing each major section of code. · Set a variable called vacname to be equal to the name of your favorite DBVac vacuum cleaner model. · Ask the user to guess the name. Allow the user to make up to three...
In a file called Conversions.java, write a program that: Asks the user to enter a double...
In a file called Conversions.java, write a program that: Asks the user to enter a double number. Stores that number into a variable called z. Casts variable z into an integer, and stores the result into a variable called z1. Creates a variable z2, and sets it equal to the integer closest to z. Creates a variable z3, and sets it equal to the floor of z. Creates a variable z4, and sets it equal to the ceiling of z....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string. Prints out four different versions of that string: The original version of the string. The upper-case version of the string. The lower-case version of the string. A version where the character at position 0 capitalized, and all other characters in lower case. For example: if the user enters string "gaNDalF12 !! AB3w", your program output should look EXACTLY like this: Please enter a string:...
Develop a python program that prompts the user to take up the quiz (which has choices)...
Develop a python program that prompts the user to take up the quiz (which has choices) with limited attempts and limited time. Suppose if the user answered wrong then they have to get numer of attempts(limited) Quiz should have questions on General knowldge & multiple choices for those questions
Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has.
Use JAVAProgram #1: Hotel Occupancy: Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A for loop should then iterate once for each floor. In each iteration of the for loop, the program should ask the user for the number of rooms of the floor and how many of them are occupied. After all of the iterations are complete the program should display how...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT