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

标签 php mysql

我目前正在使用 PHP/mySQL 开发一个测验程序(我第一次真正使用其中任何一个)。

到目前为止,我只是致力于让 mySQL 表正确运行,为此我将所有问题放在一页上的测验中。但是,现在我希望能够在每页上只提出一个问题,然后通过一次提交一个答案来取得进展。

我在测验中选择问题的方式可能有点令人困惑。它们都有一个“quiz_id”列,与它们所在的测验相对应。测验有一个“长度”列,指定实际有多少个问题。因此,对应“quiz_id”的问题可能比测验中实际存在的问题多。我随机选择要包含的问题的方式是使用“$question =“SELECT * FROM questions WHERE quiz = '$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 页面.. 不确定我是否可以使用这一页来解决所有问题,或者一旦测验开始并且您正在超越 #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 的限制,然后最后的“提交测验”按钮会导致分数页面?一路上,“下一个问题”按钮需要存储用户对当前问题的输入。

我在编写脚本时基本上需要指导,以便从一个问题进展到下一个问题。

谢谢!

最佳答案

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

我在这里根据您的要求编写了示例脚本。

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

关于php - 每页一个测验项目(php/mysql 测验程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136151/

相关文章:

php - 使用codeigniter更新mysql数据库中的多个选择值

php - CSS/HTML/PHP 显示交替数组列表(Google Chrome 扩展)

php - 使用php提取几个元素

php - 使用 PHP 和 Mysql 创建 HTML 表单

MYSQL SELECT 最后有一个条目

MySQL - 将数据从一个表复制到另一个

php - WooCommerce Rest API 返回 404 未找到

php - PDO 将输入值附加到现有值

java - 使用 FIND_IN_SET 的 Hibernate 标准

php - Kohana ORM,在模型中定义字段