Okay, first off when you explode
$questionkey .= $row['id'] . ',';
you are going to get 21 elements in the array due to the final comma, so just to be neat let's delete the last element (which will be null)
$questionkey = explode (',' , $qkey);
unset ($questionkey[20]);
Okay this array is correct now.
On second thought, that is going to cause confusion, so let's change the array so that it is exactly correct. So delete the unset line and instead modify
$questionkey .= [B] ',' . $row['id'][/B];
* * *
$questionkey = explode (',' , $qkey);
array_shift($questionkey); // remove first element of array and shift key numbers up one spot
and while we're at it let's spell "answers" and "answered" correctly :mad: Pretty sloppy for a test.
That's a lot neater. Our array is 0-19 for the 20 actual lines. Now let's print a question number before each question
$qno = ($qnum + 1);
print "Question #" . $qno . ': ' . "<b>".$row['question']."</b><BR>\n";
Next, the question sheet prints out "you failed". This is pretty easy to fix. We move a bracket to put the "you failed" if clause inside the checkit "if". So from this
print "You ansered $correct questions correctly and $incorrect questions incorrectly.";
}
if($correct > 16) { echo "<br><br>Well done you passed"; }
else { echo "<br><br>You failed"; }
To this
print "You answered $correct questions correctly and $incorrect questions incorrectly.";
if($correct > 16) {
echo "<br><br>Well done you passed";
} else {
echo "<br><br>You failed";
}
}
This would have been obvious if the code was neater, i.e. the internal "if" clauses were indented. (LOL, I promise -- no more "neatness counts" lectures -- I know you didn't write this. I'm just naturally sloppy and I have to ride myself constantly.)
I see a couple of major problems here. Not being a genius, I'm going to add a lot of "print_r" and/or "var_dump" so that I can see what is going on. This clarifies the problem, which is that the "correct answers" from the database don't match the real-life correct answers.
Gah. We are generating a random order SELECT and then comparing it to a different SELECT. I'm just going to give up on the example and rewrite it.
Here's what I came up with. This is a draft and it's very bloated, but it works as intended, including the hard part, the RAND() shuffle (which actually is nice in that it prevents one easy form of cheating). The biggest problem I had in this was keeping the one-page structure, because it was hard to figure out how to pass the variables from one side of an "if" clause to another. I did this by attaching the values to a hidden form button.
If you want to use this "as is", you need to redo the $db connection stuff. I always put the data connection information along with a stripslashes function and then put the entire file outside the "public.html" directory.
Notice I changed the "corectans" field from an identical string to a SET field with choices 'a','b','c'
I have also added a seventh database table, 'explanation', and I'm going to put in a functionality to display information about wrong answers.
My working version of this is at Daily Health Report Trivia Quiz
<?php //<!-- TRIVIA QUIZ -->
include("./../../mysql_trivia_user_connect.php");
if ($useraction != 'checkit') {
$answer = array();
$question = array();
$qnum = array();
$orgid = array();
$i = 0;
$query = "SELECT * from entrytest ORDER by RAND() LIMIT 20";
$result = mysql_query($query, $db);
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$answer[$i] = $row[5];
$question[$i] = $row['question'];
$qnum[$i] = $i;
$orgid[$i] = $row['id'];
$ans1[$i] = $row['ans1'];
$ans2[$i] = $row['ans2'];
$ans3[$i] = $row['ans3'];
$i++;
}
$user_answer = array();
$i = 0;
$j = 1;
print "<FORM ACTION='$PHP_SELF' METHOD='post'><BR>\n";
while ($j <= 20) {
print "Question #" . $j . ': ' . "<b>".$question[$i]."</b><BR>\n";
print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]' VALUE='a'>".$ans1[$i]."<BR>\n";
print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]' VALUE='b'>".$ans2[$i]."<BR>\n";
print "<INPUT TYPE='radio' NAME='user_answer[" . $i . "]' VALUE='c'>".$ans3[$i]."<P><P>\n";
$i++;
$j++;
}
//print "<INPUT TYPE='hidden' NAME='qkey' VALUE='$questionkey'>\n";
$ii=0;
while ($ii<=19) {
print "<input type='hidden' name='answer[$ii]' value='$answer[$ii]'>\n";
print "<input type='hidden' name='orgid[$ii]' value='$orgid[$ii]'>\n";
$ii++;
}
print "<INPUT TYPE='hidden' NAME='useraction' VALUE='checkit'>\n";
print "<INPUT TYPE='submit' NAME='submitted' VALUE='Check Answers'>\n";
print "</FORM><br>\n";
} else { // <!-- if submitted, check the answers on the form -->
$i=0;
$correct = 0;
$incorrect = 0;
foreach ($_POST['orgid] as $k =>$v) {
$orgid[$k] = $v;
}
print_r ($orgid);
$k = 0;
$m = 0;
$query = "SELECT 'correctans','explanation' FROM entrytest WHERE 'id' IN \"$orgid\"";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo $row['explanation'];
}
while ($k <= 19) {
if ($_POST['user_answer'][$k] == $answer[$k]) {
$correct++;
} else {
$incorrect++;
}
$k++;
}
echo "<br><br>";
print "You answered $correct questions correctly and $incorrect questions incorrectly.";
if($correct > 16) {
echo "<br><br>Well done you passed";
} elseif { ($correct > 10)
echo "<br><br>You fell a bit short of a passing grade this time. We're sure you'll do better after you brush up
on your molecular biophysics.";
} else {
echo "<br><br>Well you made a mess of that. Try reading a book instead of skateboarding tomorrow.";
}
echo "Here are the questions you missed:<br>"
}
?>
There are a few lines that seem out of place, because I have already done a little preparatory to echoing an explanation for each incorrect answer. Actually this is a working draft with the incomplete executable scraps removed so that it will function -- I just thought you'd like to go ahead and get an answer, and maybe get started working to get this functional on your db connection.
I think the database is identical to yours except for the 'correctans' field. Once you change that and get the connection set up it should work as written with your db.
I actually had fun doing this and figuring out how to pass the values from one case of the "if" conditional to the other. As a side benefit I have a functional trivia quiz that is completely inappropriate for the website I used to test it 😉