我目前正在使用PHP / MysqL编写测验程序(这是我第一次真正使用这两种方法).
直到现在,我还只是在努力使所有这些功能都能在MysqL表中正常运行,为此,我将所有问题都放在一个测验中放在一页上.但是,现在我希望每页只提出一个问题,然后一次提交一个答案就可以进步.
选择我的问题进行测验的方式可能会有些混乱.他们都有一个与他们所参加的测验相对应的“ quiz_id”列.该测验具有一个“ length”列,该列指定了它将实际遇到的问题数.因此,与“ quiz_id”相对应的问题可能比实际的问题要多.我随机选择要包含的问题的方式是“ $question =“ SELECT * FROM Questions where quiz =’$id’ORDER BY rand()LIMIT $length”;“.
现在,每页只问一个问题很麻烦.这是因为每次您进步时,只要我们还没有达到$length个问题的数量限制,就需要选择下一个随机问题.还需要增加一个计数器,以跟踪您所问的数量中的多少个问题($length个).我不确定是否需要两个单独的动作脚本.一个开始测验,另一个在问题之间进行.
<?PHP
// initialize the MysqL data
$connect = MysqL_connect('localhost', 'root', '');
$select_db = MysqL_select_db('MysqL');
// define the id and length from url
$id = MysqL_real_escape_string($_GET['id']);
$length = MysqL_real_escape_string($_GET['length']);
// query quiz table for all columns
$query_quiz = "SELECT * FROM quizzes WHERE id = '$id' LIMIT 1";
// if quiz query fails
if(!$query_quiz_result = MysqL_query($query_quiz))
{
die("Couldn't run quiz query");
}
// fetch whole array of quiz info
$quiz = MysqL_fetch_array($query_quiz_result);
//query question table for all columns
$question = "SELECT * FROM questions WHERE quiz = '$id' ORDER BY rand() LIMIT $length";
$q_result = MysqL_query($question) or die ("Couldn't run questions query");
// store queried questions as an array to pass via session variables
$q_array = array();
while($row = MysqL_fetch_assoc($q_result))
{
$q_array[] = $row;
}
session_start();
$_SESSION['quiz'] = $id;
$_SESSION['questions'] = $q_array;
$_SESSION['length'] = $length;
?>
<html>
<head>
<title>Quiz <?PHP echo MysqL_real_escape_string($_GET['id']);?></title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id="container" style="margin-left: 30px;">
<!-- create the header using the quiz id and name -->
<h1 style="text-align: center;">Quiz <?PHP echo $quiz['id'] ?>: <?PHP echo $quiz['name'] ?></h1>
<hr />
<h4>This quiz will have a total of <?PHP echo $length?> questions.</h4>
<button onClick="parent.location='start.PHP'">Begin Quiz</button>
</div>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
这是我的start.PHP页面..不知道我是否可以对所有问题使用此页面,或者一旦测验开始并且您已经超过#1,我是否需要单独的操作页面?
<?PHP
// continue the session
session_start();
// initialize the MysqL data
$connect = MysqL_connect('localhost', 'root', '');
$select_db = MysqL_select_db('MysqL');
$quiz_id = $_SESSION['quiz'];
$length = $_SESSION['length'];
$_SESSION['questions_array'] = $_SESSION['questions'];
$current_question = array_shift($_SESSION['questions_array']);
$_SESSION['counter'] = 1;
$counter = $_SESSION['counter'];
?>
<html>
<head>
<title>Quiz <?PHP echo MysqL_real_escape_string($_GET['id']);?></title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id="container" style="margin-left: 30px;">
<h3>Question <?PHP echo $counter ?> of <?PHP echo $length ?></h3>
<hr />
<h4><?PHP echo $current_question['prompt']?></h4>
<?PHP
// define query for answers for each question
$answers = 'SELECT `prompt` FROM `answers` WHERE `quiz` = '.$quiz_id.' AND `question` = '.$current_question['id'].'';
//if Failed
if(!$answers_result = MysqL_query($answers)){
die("Couldn't run answers query");
}
?>
<p style="margin-left: 50px;">
<?PHP
// if question type is "multiple choice", loop through answer choices and print them as options
if ($current_question['if_short_answer'] == 0) {
// loop through rows of answer choices
while($a_row = MysqL_fetch_array($answers_result))
{
// print each answer choice
?>
<input type='radio' name='question_<?PHP echo $current_question['id']; ?>' value='<?PHP echo $a_row['prompt']?>'><?PHP echo $a_row['prompt']?>
<br />
<?PHP
}
echo "</p>";
}
// if question type is "short answer", create text Box
elseif ($current_question['if_short_answer'] == 1) {
?>
<input type="text" name="question_<?PHP echo $current_question['id']; ?>" /><br />
</p>
<?PHP
} ?>
<?PHP
if ($counter >= 1) { ?>
<button onClick="parent.location='next.PHP'">Next Question</button>
<?PHP
}
else { ?>
<button onClick="parent.location='start.PHP'">Begin Quiz</button>
<?PHP
}
?>
</div>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
如果这是一个非常模糊或长期的问题,我深表歉意.我现在已经迷失了方向,不知道如何进行.基本上,我在问:我如何才能每页只问一个问题,逐步进行随机问题,直到达到$length的限制,然后最后的“提交测验”按钮导致进入得分页面?一直以来,“下一个问题”按钮都需要存储用户对当前问题的输入.
谢谢!
解决方法:
首先,您将在每个页面上重新创建$q_array,因此它将仅包含最后显示的问题,将不起作用
尝试
if(isset($_SESSION['questions']))
{
$_SESSION['questions']+= ", $question_id";
}