/    Sign up×
Community /Pin to ProfileBookmark

Multipochoice Quiz (20 Random Questions)

Hi people I need some advice off you please. I’m creating a quiz for the website that I’m creating for my portfolio to help me to get a graduate job.

I want to get 20 questions (at random) I think I have got this sorted now. But when I try and run the quiz the following error shows

Parse error: syntax error, unexpected end of file in C:xampphtdocsquiz.php on line 138

When I have to admit we are looking looking at this the wrong way…. I have just looked at the file more closely in dreamweaver. There is a read blob on line

This is the line of code that I think is giving me the headache

[code=php]var url = ‘quiz.php?question=<?php echo $next; ?>’;[/code]

This is the complete file of code

[code=php] <?php
//Connect with mysql
$db = new mysqli(“host”,”user”,”pass”,”db”);

//Perform the query to choose random questions
$query = $db->query(“SELECT * FROM `table` ORDER BY RAND() LIMIT 20”);

while($row = $query->fetch_assoc()):
$question = $row[‘question’];
echo $question.”<br />”;
endwhile;

//close result
$query->close();
//Close connection
$db->close();

session_start();
if(isset($_GET[‘question’])){
$question = preg_replace(‘/[^0-9]/’, “”, $_GET[‘question’]);
$next = $question + 1;
$prev = $question – 1;
if(!isset($_SESSION[‘qid_array’]) && $question != 1){
$msg = “Sorry! No cheating.”;
header(“location: start.php?msg=$msg”);
exit();
}
if(isset($_SESSION[‘qid_array’]) && in_array($question, $_SESSION[‘qid_array’])){
$msg = “Sorry, Cheating is not allowed. You will now have to start over. Haha.”;
unset($_SESSION[‘answer_array’]);
unset($_SESSION[‘qid_array’]);
session_destroy();
header(“location: start.php?msg=$msg”);
exit();
}
if(isset($_SESSION[‘lastQuestion’]) && $_SESSION[‘lastQuestion’] != $prev){
$msg = “Sorry, Cheating is not allowed. You will now have to start over. Haha.”;
unset($_SESSION[‘answer_array’]);
unset($_SESSION[‘qid_array’]);
session_destroy();
header(“location: start.php?msg=$msg”);
exit();
}
?>
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Quiz Page</title>

<script type=”text/javascript”>
function countDown(secs,elem) {
var element = document.getElementById(elem);
element.innerHTML = “You have “+secs+” seconds remaining.”;
if(secs < 1) {
var xhr = new XMLHttpRequest();
var url = “userAnswers.php”;
var vars = “radio=0″+”&qid=”+<?php echo $question; ?>;
xhr.open(“POST”, url, true);
xhr.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
alert(“You did not answer the question in the allotted time. It will be marked as incorrect.”);
clearTimeout(timer);
}
}
xhr.send(vars);
document.getElementById(‘counter_status’).innerHTML = “”;
document.getElementById(‘btnSpan’).innerHTML = ‘<h2>Times Up!</h2>’;
document.getElementById(‘btnSpan’).innerHTML += ‘<a href=”quiz.php?question=<?php echo $next; ?>”>Click here now</a>’;
}
secs–;
var timer = setTimeout(‘countDown(‘+secs+’,”‘+elem+'”)’,1000);
}

function getQuestion(){
var hr = new XMLHttpRequest();
hr.onreadystatechange = function(){
if (hr.readyState==4 && hr.status==200){
var response = hr.responseText.split(“|”);
if(response[0] == “finished”){
document.getElementById(‘status’).innerHTML = response[1];
}
var nums = hr.responseText.split(“,”);
document.getElementById(‘question’).innerHTML = nums[0];
document.getElementById(‘answers’).innerHTML = nums[1];
document.getElementById(‘answers’).innerHTML += nums[2];
}
}
hr.open(“GET”, “questions.php?question=” + <?php echo $question; ?>, true);
hr.send();
}
function x() {
var rads = document.getElementsByName(“rads”);
for ( var i = 0; i < rads.length; i++ ) {
if ( rads[i].checked ){
var val = rads[i].value;
return val;
}
}
}
function post_answer(){
var p = new XMLHttpRequest();
var id = document.getElementById(‘qid’).value;
var url = “userAnswers.php”;
var vars = “qid=”+id+”&radio=”+x();
p.open(“POST”, url, true);
p.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
p.onreadystatechange = function() {
if(p.readyState == 4 && p.status == 200) {
document.getElementById(“status”).innerHTML = ”;
alert(“Thanks, Your answer was submitted”+ p.responseText);
var url = ‘quiz.php?question=<?php echo $next; ?>’;
window.location = url;
}
}
p.send(vars);
document.getElementById(“status”).innerHTML = “processing…”;
}
window.oncontextmenu = function(){
return false;
}
</script>

</head>

<body onLoad=”getQuestion()”>
<div id=”status”>
<div id=”counter_status”></div>
<div id=”question”></div>
<div id=”answers”></div>
</div>

</script>
</body>
</html> [/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 22.2014 — Looks like you left out the closing "}" for your first isset() if block. This is one of the reasons good programmers indent their code rigorously and consistently, so that such things are easier to detect. After running your code through one of the many on-line "beautifiers" (and putting in the missing brace), it might look something like:
[code=php]
<?php
//Connect with mysql
$db = new mysqli("host", "user", "pass", "db");

//Perform the query to choose random questions
$query = $db->query("SELECT * FROM table ORDER BY RAND() LIMIT 20");

while ($row = $query->fetch_assoc()):
$question = $row['question'];
echo $question . "<br />";
endwhile;

//close result
$query->close();
//Close connection
$db->close();

session_start();
if (isset($_GET['question'])) {
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$next = $question + 1;
$prev = $question - 1;
} // <--- I added this brace. You need to double-check it logically belongs here
if (!isset($_SESSION['qid_array']) && $question != 1) {
$msg = "Sorry! No cheating.";
header("location: start.php?msg=$msg");
exit();
}
if (isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])) {
$msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
unset($_SESSION['answer_array']);
unset($_SESSION['qid_array']);
session_destroy();
header("location: start.php?msg=$msg");
exit();
}
if (isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev) {
$msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
unset($_SESSION['answer_array']);
unset($_SESSION['qid_array']);
session_destroy();
header("location: start.php?msg=$msg");
exit();
}
?>
[/code]
Copy linkTweet thisAlerts:
@stokie-ruchauthorNov 23.2014 — Looks like you left out the closing "}" for your first isset() if block. This is one of the reasons good programmers indent their code rigorously and consistently, so that such things are easier to detect. After running your code through one of the many on-line "beautifiers" (and putting in the missing brace), it might look something like:
[code=php]
<?php
//Connect with mysql
$db = new mysqli("host", "user", "pass", "db");

//Perform the query to choose random questions
$query = $db->query("SELECT * FROM table ORDER BY RAND() LIMIT 20");

while ($row = $query->fetch_assoc()):
$question = $row['question'];
echo $question . "<br />";
endwhile;

//close result
$query->close();
//Close connection
$db->close();

session_start();
if (isset($_GET['question'])) {
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$next = $question + 1;
$prev = $question - 1;
} // <--- I added this brace. You need to double-check it logically belongs here
if (!isset($_SESSION['qid_array']) && $question != 1) {
$msg = "Sorry! No cheating.";
header("location: start.php?msg=$msg");
exit();
}
if (isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])) {
$msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
unset($_SESSION['answer_array']);
unset($_SESSION['qid_array']);
session_destroy();
header("location: start.php?msg=$msg");
exit();
}
if (isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev) {
$msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
unset($_SESSION['answer_array']);
unset($_SESSION['qid_array']);
session_destroy();
header("location: start.php?msg=$msg");
exit();
}
?>
[/code]
[/QUOTE]


Thanks for this I have put the code into my file I'm now getting the following errors when I try and run the quiz.

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:xampphtdocsquiz.php on line 3

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:xampphtdocsquiz.php on line 3

Warning: mysqli::query(): Couldn't fetch mysqli in C:xampphtdocsquiz.php on line 6

Fatal error: Call to a member function fetch_assoc() on a non-object in C:xampphtdocsquiz.php on line 8

Everything else is working on my site....
×

Success!

Help @stokie-ruch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...