如何在在线测验中一次仅显示一个数据 [英] How to display only one data at a time in an online quiz
问题描述
我以前已经制作了一个文本文件,并将其转换为多维数组以显示为测验的问题.
I've previously made a text file and turned it into multidimensional array to display as the questions for my quiz.
注意::我无法插入图片,因此无法提供任何示例,因此我将尽力描述.
Note: I am unable to insert images therefore I cannot provide any example so I'll try to be as descriptive as I can.
每次用户单击我的测验时,我都尝试一次仅显示一个问题.
I'm trying to display only one question at a time, every time a user clicks on my quiz.
到目前为止,这是我的代码. main.php 页面:
This is my code so far. The main.php page:
<h2>ONLINE QUIZ</h2>
<ul>
<li><a href='question.php'>Take quiz</a></li>
<li><a href='module.php'>Admin Module</a></li>
</ul>
<?php
$file = fopen('data.txt', 'r');
$array = array();
while ($line = fgetcsv($file)) {
$array[] = $line;
}
fclose($file);
session_start();
$_SESSION["questions_array"]=$array;
?>
以及 question.php 页面:
<?php
session_start();
$array=$_SESSION["questions_array"];
foreach ($array as $q => $data) {
echo '<p>'.array_shift($data).'</p>';
foreach ($data as $a => $answer) {
echo
' <input type="radio" name="question-'.$q.'" id="question-'.$q.'"'.
' value="'.$a.'"/>'.
' <label for="question-'.$q.'">'.$answer.'</label>'.
'<br>';
}
}
?>
单击Take quiz
链接时,用户将被带到仅显示一个问题的问题页面.然后,用户选择一个答案并点击submit
.此提交按钮会将用户带到结果页面,然后他们可以点击continue
.
When the Take quiz
link is clicked, the user is taken to the question page where only one question is shown. The user then picks an answer and hits submit
. This submit button will take the user to the result page where they can then hit continue
.
Continue
链接会将其重定向回显示下一个问题的问题页面.
The Continue
link will redirect them back to the question page where the next question is displayed.
根据我之前做过的事情,我试图使用isset()
函数来实现这一目标.但是,问题是我不确定如何精确编写isset()
.
From stuff that I've done before, I am attempting to use the isset()
function to make this happen. However, the problem is that I'm not sure how exactly to write my isset()
.
我已经从该站点找到了此代码段,不确定它是否有用,但是:
I've found this snippet from this site, I'm not sure if it's useful, but:
if (!isset($_SESSION['FirstVisit'])) {
//show site for the first time part
$_SESSION['FirstVisit] = 1;
header("Location: http://example.com/index.php");
// Don't forget to add http colon slash slash www dot before!
} else { Show normal site }
但是我再次发现自己空白.我究竟该如何使用isset()
仅显示一个问题?
But once again I found myself blank. How exactly do I use the isset()
to display only one question?
推荐答案
我有点明白您的要求了,我已经阐明了基本结构.所有这些都可以在一页上完成,希望对您有所帮助.
I kinda get what your asking, Ive jotted out the basic structure. This can all be done on one page, hope it helps.
我是个好人,使用我之前的建议,这是一个完整的脚本; p
As im such a nice guy heres a complete script, using my prev suggestion ;p
<?php
session_start();
echo '<h2>ONLINE QUIZ</h2>';
//Scores
if($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['scores'])){
echo 'Basic output for scores';
echo '<pre>';
print_r($_SESSION['answers']);
echo '</pre>';
unset($_SESSION['answers']);
unset($_SESSION['question']);
}
//Session question/array is set
if(isset($_SESSION['question']) && isset($_SESSION['questions_array'])){
//Handle prev question post
if($_SERVER['REQUEST_METHOD']=='POST'){
//process prev question
$_SESSION['answers'][$_SESSION['question']-1]=(0+$_POST['answer']);
}
if($_SESSION['question'] < $_SESSION['total_question']){
$q=$_SESSION['question'];
//EDIT - Shuffle answers for output
//Hold the question into a var
$question = $_SESSION['questions_array'][$q][0];
//unset the question from the array
unset($_SESSION['questions_array'][$q][0]);
//put all the pos answers into a new array
$answers = $_SESSION['questions_array'][$q];
//shuffle the answers
shuffle($answers);
echo '<form method="POST" action="">
<h3>'.$question.'</h3>';
//loop through the answers
foreach($answers as $key=>$value){
//if the value is nothing cont to next, removed question key 0
if($value==''){continue;}else{
echo '<p><input type="radio" value="'.$value.'" name="answer">'.$value.'</p>';
}
}
echo '<p><input type="submit" value="Submit"></p>
</form>';
}else{
//Quiz Complete
echo 'Test Complete <a href="'.basename($_SERVER["SCRIPT_FILENAME"]).'?scores=1">Check scores</a>';
}
//Assign next question to session
$_SESSION['question']++;
}else{
//Pages first load so show quiz index
$_SESSION['question']=0;
get_questions();
?>
<ul>
<li><a href='<?=basename($_SERVER["SCRIPT_FILENAME"]);?>'>Take quiz</a></li>
<li><a href='module.php'>Admin Module</a></li>
</ul>
<?php
}
//Function to put questions in session
function get_questions(){
$file = fopen('data.txt', 'r');
$array = array();
while ($line = fgetcsv($file,1000,',')) {
$array[] = $line;
}
fclose($file);
$_SESSION['questions_array']=$array;
$_SESSION['total_question']=count($array);
return;
}
?>
这篇关于如何在在线测验中一次仅显示一个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!