【问题标题】:Radio buttons and checking user's choice in a quiz单选按钮和检查用户在测验中的选择
【发布时间】:2013-09-10 02:21:49
【问题描述】:

我正在制作一个测验系统,并尝试了一种从 MySQL 获取变量和值的不同方法。

$question = mysql_query("SELECT * FROM `questions`");
$stat = mysql_fetch_assoc($question);
$num = mysql_num_rows($question);
$questionid = 0;
for($i=0;$i<=$num;$i++)
{
$question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$stat = mysql_fetch_assoc($question);
//if($stat['answer'] == null
echo $stat['question'] . '<br />';
echo '<input type="radio" name="' . $i .'" value="' . $questionid . '" />' . $stat['answer1'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer2'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer3'] . '<br />';
echo '<input type="radio" name="$i" value"$questionid" />' . $stat['answer4'] . '<br />';
$questionid++;
}

现在,我想让这个人选择正确的答案,但是当我尝试在问题 1 和问题 2 中选择答案时,它可能不会让我选择正确的答案,可能是因为收音机具有相同的名称 - 我没有不知道如何让学生在每个问题中选择一个答案,以及如何获得他的选择(将其存储在变量中并检查答案是否正确)。

【问题讨论】:

    标签: mysql phpmyadmin radio-button multiple-choice


    【解决方案1】:

    您在使用 PHP 变量和引号('/ ") 时遇到问题。未正确使用连接运算符。

    name="' . $i .'" value="' . $questionid . '"

           /\ /\       /\         /\
    

    见代码:

    for($i=0;$i<=$num;$i++)
    {
    $question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
    $stat = mysql_fetch_assoc($question);
    //if($stat['answer'] == null
    echo $stat['question'] . '<br />';
    echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />';
    echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />';
    echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />';
    echo '<input type="radio" name="' . $questionid .'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />';
    $questionid++;
    }
    

    【讨论】:

    • 如果每个按钮的值都是$questionid,那么您将不知道按下了哪个答案按钮...查看我在*.com/questions/14462380/…的答案以获得更完整的解决方案
    • @Floris:我只想到引用。是的你是对的。每个单选按钮应具有相同的问题 ID 名称但不同的值,即答案。
    【解决方案2】:

    我认为你的做法不是那么好。我认为迭代代码的最佳方法是 while() 循环。喜欢

    $all = mysql_query("SELECT * FROM 'questions'");
    while($all_array=mysql_fetch_array($all))
    {
    $question = mysql_query("SELECT * FROM questions WHERE id='".$all_array['id']."'");
    while($stat=mysql_fetch_array(question)){
    echo $stat['question'] . '<br />';
    echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer1'] . '" />' . $stat['answer1'] . '<br />';
    echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer2'] . '" />' . $stat['answer2'] . '<br />';
    echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer3'] . '" />' . $stat['answer3'] . '<br />';
    echo '<input type="radio" name="' .$stat['id'].'" value="' . $stat['answer4'] . '" />' . $stat['answer4'] . '<br />';
    }
    }
    

    【讨论】:

      最近更新 更多