随机-and记住randomisation-在PHP选择题 [英] Randomizing -and remembering that randomisation- multiple choice questions in php

查看:174
本文介绍了随机-and记住randomisation-在PHP选择题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我想codeA选择题测验我的同学 - 和主要帮助我自己learning-等我创建使用PHP(08年2月5日)和MySQL多基于Web的选择题(5.0.32)

I'm trying to code a multiple choice quiz for my fellow students -and primarily to aid my own learning- and so I'm creating a multiple choice web-based quiz using PHP (5.2.08) and MySQL (5.0.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($问题),输出一个问题:

Sample print_r($questions) output for one question:

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检索提问/回答,并分配给变量:

PHP to retrieve the questions/answers and assign to variables:

  $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>

我想要做的是重新排序的答案,记得重新排序(使用-I认为 - 的 $问题[$ i] [答案] [$ C] [正确]的值来确定答案的真(1)或假('2'),但我想我迷路了杂牌组装电脑,什么地方。如果任何人有任何帮助,提供,建议,使他们倒是非常欢迎。

What I'd like to do is to reorder the answers and remember the reorder (using -I think- the value of $questions[$i][answers][$c][correct] to determine if the answer's true ('1') or false ('2'). But I think I got lost in the kludge, somewhere. If anyone has any help to offer, suggestions to make they'd be most welcome.

至于会有人正在热情地向编辑code例子下降到必需品(​​有太多,我只是不知道什么是必需的信息)。

As would anyone being kind enough to edit the code examples down to the necessities (there's way too much, I'm just not sure what's necessary information).

谢谢!

推荐答案

您的数据表是可怕的。你需要的问题之一表,另一个表答案。在回答表中的每个条目是指回在问题表的问题,并具有一个标志,指示它是否是正确的答案。

Your data tables are awful. You need one table for questions and another table for answers. Each entry in the answers table refers back to a question in the questions table and has a flag indicating whether it is the correct answer.

因此​​,问题表具有以下字段:

So, the QUESTIONS table has the following fields:


  • QUESTION_ID

  • QUESTION_TEXT

  • QUESTION_TYPE - 像MC,TF,FIB ...

  • QUESTION_ID
  • QUESTION_TEXT
  • QUESTION_TYPE -- Like MC, TF, FIB ...

解答表具有以下字段:


  • ANSWER_ID

  • QUESTION_ID

  • ANSWER_TEXT

  • IS_CORRECT

  • ANSWER_ID
  • QUESTION_ID
  • ANSWER_TEXT
  • IS_CORRECT

使您的生活更轻松无限,并省却了的存在的理由的这个问题。

Makes your life infinitely easier and obviates the raison d'etre for this question.

这篇关于随机-and记住randomisation-在PHP选择题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆