【问题标题】:One quiz item per page (php/mysql quiz program)每页一个测验项目(php / mysql测验程序)
【发布时间】:2012-08-27 04:14:15
【问题描述】:

我目前正在使用 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 的限制,然后最后的“提交测验”按钮导致分数页面?一路走来,“下一个问题”按钮需要存储用户对当前问题的输入。

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

谢谢!

【问题讨论】:

  • 我现在没有收到任何错误,因为我还没有让它做我需要它做的事情.. 基本上它不会存储任何答案或过去问题 #1 的进展对现在,我正在寻求有关如何编写将执行“下一个”功能的脚本的指导

标签: php mysql


【解决方案1】:

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

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

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

【讨论】:

  • 请避免仅链接答案,因为如果链接变为非活动状态,答案将无效。也在这里发布答案的基本部分。
【解决方案2】:

首先,您要在每个页面上重新创建 $q_array,因此它只包含最后一个显示的问题,该问题不起作用

试试

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

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

【讨论】:

  • 我想知道为什么它会随着我的进步而改变问题。感谢您指出这一点!您认为是否需要两个脚本,或者我可以制作一个吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 2022-07-06
  • 1970-01-01
相关资源
最近更新 更多