create a random question generator php

Solutions on MaxInterview for create a random question generator php by the best coders in the world

showing results for - "create a random question generator php"
Anton
12 Jun 2018
1  <?php
2    //Connect with mysql
3    $db = new mysqli("host","user","pass","db");
4
5    //Perform the query to choose random questions
6    $query = $db->query("SELECT * FROM `table` ORDER BY RAND() LIMIT 20");
7
8    while($row = $query->fetch_assoc()):
9        $question = $row['question'];
10        echo $question."<br />";
11    endwhile;
12
13    //close result
14    $query->close();
15    //Close connection
16    $db->close();
17
18    session_start();
19    if(isset($_GET['question'])){
20    $question = preg_replace('/[^0-9]/', "", $_GET['question']);
21    $next = $question + 1;
22    $prev = $question - 1;
23    if(!isset($_SESSION['qid_array']) && $question != 1){
24    $msg = "Sorry! No cheating.";
25    header("location: start.php?msg=$msg");
26    exit();
27    }
28    if(isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])){
29    $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
30    unset($_SESSION['answer_array']);
31    unset($_SESSION['qid_array']);
32    session_destroy();
33    header("location: start.php?msg=$msg");
34    exit();
35    }
36    if(isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev){
37    $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
38    unset($_SESSION['answer_array']);
39    unset($_SESSION['qid_array']);
40    session_destroy();
41    header("location: start.php?msg=$msg");
42    exit();
43    }
44    ?>
45    <!doctype html>
46    <html>
47    <head>
48    <meta charset="utf-8">
49    <title>Quiz Page</title>
50
51    <script type="text/javascript">
52    function countDown(secs,elem) {
53    var element = document.getElementById(elem);
54    element.innerHTML = "You have "+secs+" seconds remaining.";
55    if(secs < 1) {
56    var xhr = new XMLHttpRequest();
57    var url = "userAnswers.php";
58    var vars = "radio=0"+"&qid="+<?php echo $question; ?>;
59    xhr.open("POST", url, true);
60    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
61    xhr.onreadystatechange = function() {
62    if(xhr.readyState == 4 && xhr.status == 200) {
63    alert("You did not answer the question in the allotted time. It will be marked as incorrect.");
64    clearTimeout(timer);
65    }
66    }
67    xhr.send(vars);
68    document.getElementById('counter_status').innerHTML = "";
69    document.getElementById('btnSpan').innerHTML = '<h2>Times Up!</h2>';
70    document.getElementById('btnSpan').innerHTML += '<a href="quiz.php?question=<?php echo $next; ?>">Click here now</a>';
71    }
72    secs--;
73    var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
74    }
75
76    function getQuestion(){
77    var hr = new XMLHttpRequest();
78    hr.onreadystatechange = function(){
79    if (hr.readyState==4 && hr.status==200){
80    var response = hr.responseText.split("|");
81    if(response[0] == "finished"){
82    document.getElementById('status').innerHTML = response[1];
83    }
84    var nums = hr.responseText.split(",");
85    document.getElementById('question').innerHTML = nums[0];
86    document.getElementById('answers').innerHTML = nums[1];
87    document.getElementById('answers').innerHTML += nums[2];
88    }
89    }
90    hr.open("GET", "questions.php?question=" + <?php echo $question; ?>, true);
91    hr.send();
92    }
93    function x() {
94    var rads = document.getElementsByName("rads");
95    for ( var i = 0; i < rads.length; i++ ) {
96    if ( rads[i].checked ){
97    var val = rads[i].value;
98    return val;
99    }
100    }
101    }
102    function post_answer(){
103    var p = new XMLHttpRequest();
104    var id = document.getElementById('qid').value;
105    var url = "userAnswers.php";
106    var vars = "qid="+id+"&radio="+x();
107    p.open("POST", url, true);
108    p.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
109    p.onreadystatechange = function() {
110    if(p.readyState == 4 && p.status == 200) {
111    document.getElementById("status").innerHTML = '';
112    alert("Thanks, Your answer was submitted"+ p.responseText);
113    var url = 'quiz.php?question=<?php echo $next; ?>';
114    window.location = url;
115    }
116    }
117    p.send(vars);
118    document.getElementById("status").innerHTML = "processing...";
119    }
120    window.oncontextmenu = function(){
121    return false;
122    }
123    </script>
124
125
126    </head>
127
128    <body onLoad="getQuestion()">
129    <div id="status">
130    <div id="counter_status"></div>
131    <div id="question"></div>
132    <div id="answers"></div>
133    </div>
134
135
136    </script>
137    </body>
138    </html> 
139
similar questions
queries leading to this page
create a random question generator php