每页一个测试项目(php/mysql测试程序)


One quiz item per page (php/mysql quiz program)

我目前正在使用PHP/mySQL编写一个测试程序(这是我第一次真正使用这两种程序)。

到目前为止,我只是在努力让mySQL表正确运行,为此,我把所有问题都放在一个页面上进行测试。然而,现在我希望能够在每页上只提出一个问题,然后一次提交一个答案。

我的问题在测验中的选择方式可能有点令人困惑。他们都有一个"quiz_id"列,与他们参加的测验相对应。测验有一个指定实际问题数量的"长度"列。因此,带有相应"quiz_id"的问题可能比测验中实际出现的问题更多。我随机选择将包括哪些问题的方法是使用"$question="select*FROM questions WHERE question='$id'ORDER BY rand()LIMIT$length";"。

现在我在每页只提出一个问题时遇到了很多麻烦。这是因为每次你进步,都需要选择下一个随机问题,只要我们还没有达到问题数量$length的限制。计数器也需要增加,以跟踪你在问什么数字问题,而不是多少($length)。我不确定是否需要两个单独的动作脚本。。一个开始测验,然后一个在问题之间进行。

这是我的quiz.php页面(任何测验的起始页):

<?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页面。。不确定我是否可以用这一页来回答所有问题,或者我是否需要一个单独的动作页来回答测验开始后你的问题?

<?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的限制,然后在末尾的"提交测验"按钮会导致一个分数页?一直以来,"下一个问题"按钮都需要存储用户对当前问题的输入。

我在写剧本时基本上需要指导,从一个问题到下一个问题。

谢谢!

您只需要从数据库中获取所有问题,使用jquery show()/hide()方法一次只显示一个问题。

我已经在这里为您的要求编写了示例脚本。

http://www.smarttutorials.net/responsive-quiz-application-using-php-mysql-jquery-ajax-and-twitter-bootstrap/

首先,在每页上重新创建$q_array,这样它只包含最后一个显示的问题,这将不起的作用

尝试

if(isset($_SESSION['questions']))
{
   $_SESSION['questions']+= ", $question_id";       
}

在选择查询中,应省略数组中已显示的问题。