Hi Thomas
Here is the (almost) complete listing of the offending script. The only things missing are bits of irrelevant HTML. Plus, it looks a bit untidy as all my nice tabs have been decimated.
I hope you understand it, and fingers crossed, that you can see where I have gone wrong.
<html>
<head>
<title>General Knowledge Quiz</title>
</head>
<body>
<div align="center"><h3>General Knowledge Quiz</h3>
<h2>Questions</h2></div>
include "contentdb.php";
global $quiz;
$questions = mysql_query("SELECT * FROM $quiz_table ORDER BY id",$db);
$total_questions = mysql_num_rows($questions);
if ($_POST[sub_button] != "yes"){
// Random number up to max records in Table.
$x = array();
while ($i < 16){
$temp_x = mt_rand(1, $total_questions);
if (!in_array($temp_x, $x)) {
$x[$i++] = $temp_x;
}
}
// Select each of the 15 records (Questions & answers) according to random numbers above.
foreach ($x as $key => $current){
if ($key > 0){
$display = mysql_query("SELECT * FROM $quiz_table WHERE id = $current",$db);
echo "<form method='POST' action='quizphp.php'>";
echo "<table width='96%' border='0' cellspacing='0' cellpadding='3' align='center'>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$answer = $row["answer"];
$option = "id_row" . $id . "[key]";
// Displays the Questions and 4 possible answers for each question.
echo "<tr>";
echo "<td width='100%' colspan='8'><font style='color:red; font-size:7pt'>$id <font style='color:navy; font-size:10pt'>$question</font></td>";
echo "</tr>";
echo "<tr>";
echo "<td width='22%'><font style='color:blue; font-size:11pt'>$opt1</font></td>";
echo "<td width='3%'><font style='color:red; font-size:7pt'>1</font><input type='radio' name='$option' value=$opt1 checked></td>";
echo "<td width='22%'><font style='color:blue; font-size:11pt'>$opt2</font></td>";
echo "<td width='3%'><font style='color:red; font-size:7pt'>2</font><input type='radio' name='$option' value=$opt2></td>";
echo "<td width='22%'><font style='color:blue; font-size:11pt'>$opt3</font></td>";
echo "<td width='3%'><font style='color:red; font-size:7pt'>3</font><input type='radio' name='$option' value=$opt3></td>";
echo "<td width='22%'><font style='color:blue; font-size:11pt'>$opt4</font></td>";
echo "<td width='3%'><font style='color:red; font-size:7pt'>4</font><input type='radio' name='$option' value=$opt4></td>";
echo "</tr>";
}
}
}
echo "</table>";
echo "<input type='hidden' name='first_time' value='no'>";
echo "<input type='hidden' name='sub_button' value='yes'>";
echo "<input type='hidden' name='x_array' value='" . join('|', $x) . "' >";
echo "<br><center><input type='submit' value='Check your Score' name='submit'></center>";
echo "</form>";
}
else if ($_POST[sub_button] == "yes"){
$xx = explode('|', $_POST['x_array']);
[code=php]
// Test code only.
echo "<br>Button is pressed!<br>";
foreach ($xx as $key => $current){
if ($key > 0){
echo "ID= $key is $current <br>";
}
}
// Above is CORRECT!
// In the section below, I want to determine which option (1 - 4) the user has clicked for each question. So I can see if the answer is correct?
foreach ($xx as $key => $current){
if ($key > 0){
$display = mysql_query("SELECT * FROM $quiz_table WHERE id = $current",$db);
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$answer = $row["answer"];
$quiz = array(
'id' => $id,
'question' => $question,
'opt1' => $opt1,
'opt2' => $opt2,
'opt3' => $opt3,
'opt4' => $opt4,
'answer' => $answer);
echo "$quiz[id] - $quiz[question] - $quiz[opt1] - $quiz[opt2] - $quiz[opt3] - $quiz[opt4] - <b>$quiz[answer]</b> <br>";
}
}
}
}
</body>
</html>
Table details (MySQL)
id tinyint(4) PRI
question varchar(255)
opt1 varchar(255)
opt2 varchar(255)
opt3 varchar(255)
opt4 varchar(255)
answer varchar(255)
I've often wondered how that was done?
Terry