View Full Version : How to compare user form selection, with correct answer?

12-31-2006, 08:34 PM
I've searched the forum regarding comparing and arrays and am coming up short. I've also asked various PHP'ers to no avail - either they give me code that is beyond my level virtually rewriting my script, or their details are vague.

So if you have any idea, much appreciated and will give you credit.

Ok, I'm working on a quiz script. The one-page version works great:


Get an answer wrong, it tells you the right answer. BUT! It does not tell you the *question* to which you answered wrong -- that is what I'm trying to do. You can view the source file and neccessary files inside:

*Now for the 2-page version, which I would prefer to focus on* -- this version carries over the user input onto "results.php" - because I don't want the form radio buttons to show, I just wanna keep it clean:


This version will echo correctly *per each question you got wrong* - however, it says "illegal offset". I read about this, and it was something about keys and values... I can't recall. So, my friend provided me with this question/answer layout:

$questions[1]['q']="The 'F-16D' variant is:";
$questions[1]['a']['1']="double-seated for training";
$questions[1]['a']['2']="designed for low-level attack";
$questions[1]['a']['3']="made specifically for the 'Thunderbirds' aerobatic team";

// This bit means that the answer with ['1'] is correct (double-seated for training)

I do not have this version of the questions/answers uploaded for show, but it succesfully displays each question/answer set (a set is $questions[1], $questions[2], and so on) with radio buttons for each and all that. Perfect.

But it displays *nothing* when submitted - it should tell you the right answer, and the question. I am using the same code as I used in the 2-page and single version, to display the correct answer, for each question the user got wrong:

1st, to loop through each Q/A set:

if(isset($_POST['submit'])) {

// Loop through each answer, defined by $a (QA.INC)...
for($l=1; $l <=count($questions['a']); $l++){

Then, if user's selection does not match the correct answer:

// Display incorrect answers
if($_POST[$l] != $questions['a'][$l]['1']) {
$score = $score +0;

elseif ($questions[1]['a'][$value][$i] == $questions[1]['a'][$questions[1]['c']]) {
$score = $score +1;

/* This block was used but not currently, been fiddling around
for($i=1; $i <= count($questions); $i++){
echo("</BR><SPAN class='question'><B>" . $i . ".</B>" . " " . $questions[$i]['q'] . "</SPAN></BR>");
echo("</BR><SPAN class='incorrect'>Incorrect!</SPAN> The right answer is: " . $questions['a'][$l]['1'] . "</BR>");

It probably gets confusing, and I'm sure there's better ways to code this - but I am not looking for a new script to replace mine. All I need is a point in the right direction as to what function(s) to use - that will make the script say User chose the wrong answer for questions 2 and 3, now I'm going to display questions 2 and 3, and each right answer -- naturally this would be done with a loop, as it has done for displaying the form output in the first page (quiz.php).

Appreciate your help!