I am new to cgi, but with the help of several tutorials and forum searches I have been able to customize this script until this point. The quiz script already displays the questions randomly, but the answer choices are displayed in the order that I enter them. What do I need to do to tell it to make the answer choices display randomly? I have tried everything I can think of, but nothing has worked. Any help is GREATLY appreciated! Here's the entire sub:
sub ShowQuiz {
my $quiz = shift;
my $num_ques = getNumLine("$quiz.db");
$limit = param('number') > 0 ? param('number') : $num_ques;
$limit = $num_ques if $limit > $num_ques;
for ($i=0;$i<$num_ques;$i++) {$array[$i] = $i;}
for ($i=0;$i<$num_ques;$i++) {
$x = $array[int(rand($num_ques))];
$y = $array[int(rand($num_ques))];
$temp = $array[$x];
$array[$x] = $array[$y];
$array[$y] = $temp;
}
splice(@array, $limit);
flock(2, DB);
open(DB, "$db_dir/$quiz.db") or error("Cannot open $quiz.db: $!");
@lines = <DB>;
close(DB);
flock(8, DB);
print "<form action=\"$script\" method=\"POST\">\n";
print "<table border=0 cellspacing=0 cellpadding=0 bgcolor=#FFFFFF width=95%><tr><td>\n";
print "<table border=0 cellspacing=1 cellpadding=4 width=100%>\n";
print "<tr><td bgcolor=#FFFFFF><font$font_para size=4><b><i>".getQuizName($quiz)."</i></b></font><br></td></tr>\n";
$i = 1;
foreach $linenum (@array) {
my $j = 'color' . (($i % 2) + 1);
($qtype, $question, $qimage, $answer, $aimage, $explanation, $choice) = split(/\|\|/, $lines[$linenum]);
print "<tr bgcolor=#DFE9F9><td width=100%><font$font_para size=2><b>$i. $question</b>";
print "<br><img src=\" if $qimage ne "";
if ($qtype eq "mc") {
chomp($choice);
@choices = split(/\`\`/, $choice);
foreach $choice (@choices) {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
}
}
if ($qtype eq "sa") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30>";
}
if ($qtype eq "nu") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30> (Enter only numeric value)";
}
if ($qtype eq "tf") {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"t\">True <input type=\"radio\" name=\"$linenum\" value=\"f\">False";
}
print "</font></td></tr>\n";
$i++;
}
sub ShowQuiz {
my $quiz = shift;
my $num_ques = getNumLine("$quiz.db");
$limit = param('number') > 0 ? param('number') : $num_ques;
$limit = $num_ques if $limit > $num_ques;
for ($i=0;$i<$num_ques;$i++) {$array[$i] = $i;}
for ($i=0;$i<$num_ques;$i++) {
$x = $array[int(rand($num_ques))];
$y = $array[int(rand($num_ques))];
$temp = $array[$x];
$array[$x] = $array[$y];
$array[$y] = $temp;
}
splice(@array, $limit);
flock(2, DB);
open(DB, "$db_dir/$quiz.db") or error("Cannot open $quiz.db: $!");
@lines = <DB>;
close(DB);
flock(8, DB);
print "<form action=\"$script\" method=\"POST\">\n";
print "<table border=0 cellspacing=0 cellpadding=0 bgcolor=#FFFFFF width=95%><tr><td>\n";
print "<table border=0 cellspacing=1 cellpadding=4 width=100%>\n";
print "<tr><td bgcolor=#FFFFFF><font$font_para size=4><b><i>".getQuizName($quiz)."</i></b></font><br></td></tr>\n";
$i = 1;
foreach $linenum (@array) {
my $j = 'color' . (($i % 2) + 1);
($qtype, $question, $qimage, $answer, $aimage, $explanation, $choice) = split(/\|\|/, $lines[$linenum]);
print "<tr bgcolor=#DFE9F9><td width=100%><font$font_para size=2><b>$i. $question</b>";
print "<br><img src=\" if $qimage ne "";
if ($qtype eq "mc") {
chomp($choice);
@choices = split(/\`\`/, $choice);
foreach $choice (@choices) {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
}
}
if ($qtype eq "sa") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30>";
}
if ($qtype eq "nu") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30> (Enter only numeric value)";
}
if ($qtype eq "tf") {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"t\">True <input type=\"radio\" name=\"$linenum\" value=\"f\">False";
}
print "</font></td></tr>\n";
$i++;
}