首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每页一个测验项目(php/mysql测验程序)

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

Stack Overflow用户
提问于 2012-08-27 12:14:16
回答 2查看 13.9K关注 0票数 0

我目前正在编写一个使用PHP/mySQL的测验程序(这是我第一次实际使用这两种语言)。

到目前为止,我只是在努力让所有的mySQL表都能正常工作,为了做到这一点,我把所有的问题放在一个页面上的测验中。但是,现在我希望能够在每页上只提出一个问题,然后通过一次提交一个答案来继续。

我的问题被选在测验中的方式可能有点令人困惑。他们都有一个"quiz_id“列,与他们所在的测验相对应。测验有一个"length“列,它指定了实际有多少个问题。因此,带有相应"quiz_id“的问题可能会比测验中实际出现的问题更多。我随机选择要包含的问题的方法是使用"$question = "SELECT * FROM quiz quiz quiz= '$id‘ORDER BY rand() LIMIT $length";“。

现在,我在每页只提出一个问题时遇到了很多麻烦。这是因为,只要我们没有达到$length问题数量的限制,就需要选择下一个随机问题。还需要增加一个计数器,以跟踪您在多少个问题上($length)中的多少个问题。我不确定是否需要有两个独立的动作脚本。一个用于开始测验,另一个用于在两个问题之间进行。

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

代码语言:javascript
运行
复制
<?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之后?

代码语言:javascript
运行
复制
<?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的限制,然后在结束时的“提交测验”按钮指向分数页面?在此过程中,“下一个问题”按钮需要存储用户对当前问题的输入。

我基本上需要指导来写我的脚本,从一个问题到下一个问题。

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-27 12:39:29

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

试一试

代码语言:javascript
运行
复制
if(isset($_SESSION['questions']))
{
   $_SESSION['questions']+= ", $question_id";       
}

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

票数 0
EN

Stack Overflow用户

发布于 2013-09-28 11:18:55

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

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12136151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档