【问题标题】:Randomizing -and remembering that randomisation- multiple choice questions in php随机化 - 并记住随机化 - php中的多项选择题
【发布时间】:2009-06-22 17:14:04
【问题描述】:

问题:

我正在尝试为我的同学编写一个多项选择测验 - 主要是为了帮助我自己的学习 - 所以我正在使用 PHP (5.2.08) 和 MySQL (5.0) 创建一个基于 Web 的多项选择测验.32)

问题表是:

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(6)       | NO   | PRI | NULL    | auto_increment | 
| question | varchar(200) | NO   |     | NULL    |                | 
| correct  | varchar(80)  | NO   |     | NULL    |                | 
| wrong1   | varchar(80)  | NO   |     | NULL    |                | 
| wrong2   | varchar(80)  | NO   |     | NULL    |                | 
| wrong3   | varchar(80)  | NO   |     | NULL    |                | 
+----------+--------------+------+-----+---------+----------------+

一个问题的示例 print_r($questions) 输出:

Array
(
    [0] => Array
        (
            [id] => 1
            [question] => What is the correct pipeline pressure for Nitrous Oxide (<abbr title="Nitrous Oxide.">N<span class="chem-notation">2</span>O</abbr>)?
            [answers] => Array
                (
                    [0] => Array
                        (
                            [correct] => 1
                            [answer] => 60<abbr title="Pounds per square inch">PSI</abbr>.
                        )
                    [1] => Array
                        (
                            [correct] => 0
                            [answer] => 45<abbr title="Pounds per square inch">PSI</abbr>.
                        )
                    [2] => Array
                        (
                            [correct] => 0
                            [answer] => 30<abbr title="Pounds per square inch">PSI</abbr>.
                        )
                    [3] => Array
                        (
                            [correct] => 0
                            [answer] => 15<abbr title="Pounds per square inch">PSI</abbr>.
                        )
                )
        )

PHP 检索问题/答案并分配给变量:

  $results = $results2 = mysql_query("
    SELECT questions.id AS id,
     questions.question AS q,
     questions.correct AS c,
     questions.wrong1 AS w1,
     questions.wrong2 AS w2,
     questions.wrong3 AS w3
    FROM questions
    ORDER BY questions.id
    LIMIT 40")
    or die("Oops, unable to access database at this time." . mysql_error());

 while ($row = mysql_fetch_array($results)) {
  if (!isset($i)) {
   $i = 0;
  }
  else {
   $i = $i;
  } 

  $answers[$i]  = array(
                  0=>array (correct => 1, answer => $row['c']),
                  1=>array (correct => 0, answer => $row['w1']),
                  2=>array (correct => 0, answer => $row['w2']),
                  3=>array (correct => 0, answer => $row['w3'])
                  );

  $questions[$i] = array(id=>$row['id'],
                   question=>$row['q'],
                   answers=>$answers[$i]);

  $correctAnswer[$i]    = array($row['c']);
            $i++;
 }

显示问题/答案:

<?php

require_once 'incs/dbcnx.php';
require_once 'incs/questions.php';

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Multiple choice questions for ODP students.</title>
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />

</head>

<body>

<?php

$submitted  = $_POST['submit'];
$quesions   = $_SESSION['questions'];
$correctAnswers = $_SESSION['correctAnswer'];

if (isset($submitted) && $submitted == "1") {

    // display the results.

echo "<form>";
    for ($i=0;$i<sizeof($questions);$i++) {

        echo "\t\t<fieldset>\n\n";

        echo "\t\t<label>\n\t\t\t<span class=\"qNum\">Q" . $questions[$i][id] . ": </span>\n\t\t\t";
        echo $questions[$i][question] . "\n\t\t</label>\n";

            $submittedName = (string) "question" . $questions[$i][id];

        for ($c=0;$c<sizeof($questions[$i][answers]);$c++) {

            if ($_POST["$submittedName"] == $c) {
                if ($questions[$c][answers][$c][correct] == 1) {
                    echo "\n\t\t<span class=\"correct\"><span class=\"hint\">✓</span>";
                    echo "<input checked type=\"radio\" name=\"question" . $questions[$i][id] . "\"";
                    echo " value=\"$c\" />";
                }
                else {
                    echo "\n\t\t<span class=\"submitted\"><span class=\"hint\">✗</span>";
                    echo "<input type=\"radio\" name=\"question" . $questions[$i][id] . "\"";
                    echo " value=\"$c\" />";
                }
            }
            elseif ($questions[$c][answers][$c][correct] == 1) {
                echo "\n\t\t<span class=\"thisOne\">";
                echo "<input type=\"radio\" name=\"question" . $questions[$i][id] . "\"";
                echo " value=\"$c\" />";
            }
            else {
                echo "\n\t\t<span class=\"optionLine\">";
                echo "<input disabled type=\"radio\" name=\"question" . $questions[$i][id] . "\"";
                echo " value=\"$c\" />";
            }

            echo $questions[$i][answers][$c][answer] . "</span>";
        }
        echo "\n\n\t\t</fieldset>\n\n";
    }

echo "</form>";

}

else {
    // show the form
?>
    <form enctype="form/multipart" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<?php

    for ($i=0;$i<sizeof($questions);$i++) {

        echo "\t\t<fieldset>\n\n";

        echo "\t\t<label>\n\t\t\t<span class=\"qNum\">Q" . $questions[$i][id] . ": </span>\n\t\t\t";
        echo $questions[$i][question] . "\n\t\t</label>\n";


        for ($c=0;$c<sizeof($questions[$i][answers]);$c++) {
            echo "\n\t\t<span class=\"optionLine\">";
            echo "<input type=\"radio\" name=\"question" . $questions[$i][id] . "\"";
            echo " value=\"$c\" />";
            echo $questions[$i][answers][$c][answer] . "</span>";
        }
        echo "\n\n\t\t</fieldset>\n\n";
    }



?>

    <fieldset>

        <input type="reset" value="clear" />
        <input type="submit" value="submit" />
        <input type="hidden" name="submit" value="1" />

    </fieldset>

    </form>
<?php

}
?>

<div id="variables">
</div>
</body>

</html>

我想做的是重新排序答案并记住重新排序(使用-我认为-$questions[$i][answers][$c][correct] 的值来确定答案是真('1')还是假('2')。但我想我在某个地方迷失了方向。如果有人可以提供任何帮助,欢迎提出建议。

任何人都愿意将代码示例编辑到必要的程度(太多了,我只是不确定什么是必要的信息)。

谢谢!

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    你的数据表很糟糕。你需要一张表来回答问题,另一张表来回答。答案表中的每个条目都引用问题表中的一个问题,并带有一个标志,指示它是否是正确答案。

    所以,QUESTIONS 表有以下字段:

    • QUESTION_ID
    • QUESTION_TEXT
    • QUESTION_TYPE -- 像 MC、TF、FIB ...

    ANSWERS 表有以下字段:

    • ANSWER_ID
    • QUESTION_ID
    • ANSWER_TEXT
    • IS_CORRECT

    让您的生活变得无比轻松,并消除了这个问题的存在理由

    【讨论】:

    • 我在制定餐桌计划时想到了这一点。我不记得为什么我认为不规范化会更简单。 O.o
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多