我有一个包含以下字段和示例的数据库表:
QuestionID
Question
AnswerA
AnswerB
AnswerC
AnswerD
CorrectAnswer
例子:
QuestionID: 1
Question: What is 2 + 2?
AnswerA: 1
AnswerB: 2
AnswerC: 3
AnswerD: 4
CorrectAnswer: 4
如果我的数据库中有 20 个问题,我将如何得到它,以便不仅问题以随机顺序出现,而且答案出现在不同的单选按钮上(以防止用户记住正确答案的位置)。这将被集成到 Facebook 应用程序中。下面是我的 SQL 查询:
$sql="SELECT DISTINCT Question, QuestionID, AnswerA, AnswerB, AnswerC, AnswerD FROM miniproj ORDER BY rand() LIMIT 1";
这是while
我正在使用的语句(我理解mysql_fetch_array
已弃用,但我可以再次排序):
while ($myrow = mysql_fetch_array($result))
{
$answers = array($myrow['AnswerA'], $myrow['AnswerB'], $myrow['AnswerC'], $myrow['AnswerD']);
shuffle($answers);
echo("<form action='question.php' method='POST'>\n");
echo("<h1>");
echo("Q: ");
echo($myrow['Question']);
echo("</h1>");
echo("<input type='radio' name='comments' value='A'>\n");
echo($myrow['AnswerA']);
echo("<p>");
echo("<input type='radio' name='comments' value='B'>\n");
echo($myrow['AnswerB']);
echo("<p>");
echo("<input type='radio' name='comments' value='C'>\n");
echo($myrow['AnswerC']);
echo("<p>");
echo("<input type='radio' name='comments' value='D'>\n");
echo($myrow['AnswerD']);
echo("<p>");
echo("<br />");
echo("<input type='submit' value='Submit'>\n");
echo("</form>\n");
}
任何提示都会很棒