我在向用户显示多项选择题时遇到问题。我有以下表格:
multiple_choice_questions (quiz_id,question,c_answer) 这张表有正确答案的问题
multiple_choice_answers (answer_id,quiz_id,answer) 这个表有外键quiz_id的答案
multiple_choice_info (info_id,quiz_id,名称,课程,课程,主题,备注,日期) 这里是每个测验的基本信息,实际上有助于从用户中选择适当的测验。例如如果用户将数学作为课程,则用“课程”数学来表示测试
我想显示例如10个随机问题及其答案。请注意,每个问题都有多个不同的答案。例如问题1有3个不同的答案。问题2,5个不同的答案。
我努力捕获与quiz_id相关的数据,但这种方法是错误的。我是php,mysql的新手。
<?php
include('../connect_db.php');
$sql = "select * from multiple_choice_questions
inner join multiple_choice_answers on (multiple_choice_questions.quiz_id = multiple_choice_answers.quiz_id)
inner join multiple_choice_info on (multiple_choice_questions.quiz_id = multiple_choice_info.quiz_id)";
$myData3 = mysql_query($sql);
while($row = mysql_fetch_array($myData3)) {
echo '<input type="text" value="'.$row['question'].'" name="q'.$row['quiz_id'].'"/><br>';
echo '<input type="radio" value="'.$row['answer'].'" name="q'.$row['answer'].'"/>"'.$row['answer'].'"';
}
?>
答案 0 :(得分:0)
以下面的方式格式化您的查询。这将随机给你10行。
$sql = "select A.question,B.answer,A.quizid from multiple_choice_questions AS A
inner join multiple_choice_answers AS B on (A.quiz_id = B.quiz_id)
inner join multiple_choice_info AS C on (C.quiz_id = A.quiz_id)
ORDER BY RAND() LIMIT 10";
$myData3 = mysql_query($sql);
您可以尝试以下脚本。
$sql = "select A.question,B.answer,A.quizid from multiple_choice_questions AS A
inner join multiple_choice_answers AS B on (A.quiz_id = B.quiz_id)
inner join multiple_choice_info AS C on (C.quiz_id = A.quiz_id)
ORDER BY A.quizid,RAND()";
$myData3 = mysql_query($sql);
$lastQues = 0;
$totalQues = 0;
while($row = mysql_fetch_array($myData3)) {
if($totalQues == 10)break;
if($lastQues != $row['quiz_id'])
{
echo '<input type="text" value="'.$row['question'].'" name="q'.$row['quiz_id'].'"/><br>';
$totalQues++;
}
echo '<input type="radio" value="'.$row['answer'].'" name="q'.$row['answer'].'"/>"'.$row['answer'].'"';
$lastQues = $row['quiz_id'];
}