-1

我在向用户显示多项选择题时遇到问题。我有以下表格:

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,name,class, course, subject, notes,date) 这里是每个测验的基本信息,实际上有助于从用户那里选择适当的测验。例如,如果用户将数学作为课程数学,它会显示带有“课程”数学的测试

我想显示例如 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'].'"';

}
?>     
4

1 回答 1

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'];

}
于 2012-11-02T11:18:34.387 回答