You are still dealing with a var-var on the test processing side. You can loop through the db questions and output your form fields with static names, but when you process the form, you will need to use a var-var as you loop through the form questions. The var-var will be used in the loop in place of the form field name:
================================
<?php
// connect to db
$db = mysql_connect("localhost", "user", "password");
mysql_select_db("database",$db);
// if this is not the post from the test form
if ($useraction != 'checkit') {
$query = "select * from multiple_choice";
$result = mysql_query($query, $db);
$qnum = 0;
print "<FORM ACTION='$PHP_SELF' METHOD='post'><BR>\n";
while ($row = mysql_fetch_array($result)) {
$question_formfield = 'Q' . $qnum;
print "Q. ".$row['question']."<BR>\n";
print "<INPUT TYPE='radio' NAME='$question_formfield'>$row['answera'] VALUE='A'><BR>\n";
print "<INPUT TYPE='radio' NAME='$question_formfield'>$row['answerb'] VALUE='B'><BR>\n";
print "<INPUT TYPE='radio' NAME='$question_formfield'>$row['answerc'] VALUE='C'><BR>\n";
print "<INPUT TYPE='radio' NAME='$question_formfield'>$row['answerd'] VALUE='D'><P><P>\n";
$questionkey .= $row['question_id'] . ',';
$qnum++;
}
print "<INPUT TYPE='hidden' NAME='qkey' VALUE='$questionkey'>\n";
print "<INPUT TYPE='hidden' NAME='useraction' VALUE='checkit'>\n";
print "<INPUT TYPE='submit' VALUE='Check Test'>\n";
print "</FORM><br>\n";
// check the answeres on the form
else {
$correct = 0;
$incorrect = 0;
// load answers into array so we don't overwork the db
$query = "select * from multiple_choice";
$result = mysql_query($query, $db);
while ($row = mysql_fetch_array($result)) {
$answer_array['question_id'] = $row['real_answer'];
}
// explode question id list from hidden
// form field into an array we can use
$questionkey = explode (',' , $qkey);
// now read through the form questions and check answers
$qnum = 0;
$question_formfield = 'Q' . $qnum;
while (is_set(${$question_formfield})) { // note how we use a var-vars here!
if (${$question_formfield} == $answer_array[$questionkey[$qnum]]) {
$correct++;
}
else {
$incorrect++
}
$qnum++;
$question_formfield = 'Q' . $qnum;
}
print "You ansered $correct questions correctly and $incorrect questions incorrectly.";
}
?>
HTH
-- Rich Rijnders
-- Irvine, CA US