屏幕测验中的PHP测验 [英] PHP quiz with on screen results

查看:58
本文介绍了屏幕测验中的PHP测验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据我发现的教程创建一个简单的测验. http://css-tricks.com/building-a-simple-quiz/

I am trying to create a simple quiz based on a tutorial I found. http://css-tricks.com/building-a-simple-quiz/

不幸的是,这使我无法自拔,答案很简单.

Unfortunately, this is making me spin my wheels and the answer is probably pretty simple.

我做得很好.我想更改功能.我不希望它计算答案,而是希望它做其他事情.

I got this working perfectly. I would like to change the functionality. Instead of it counting the answers, I want it to do something else.

  1. 我想再次显示问题.
  2. 此外,还要选择答案和正确答案.回显答案,字母A,B,C,D.
  1. I would like to display the questions again.
  2. Also, with the chosen answer and the correct answer. echo the answer, not the letter A,B,C,D.

对我来说,进行测验并说您错过了2而没有显示错过了哪些问题,对我来说似乎很愚蠢.

It seems silly to me to have a quiz and say you missed 2 and not show what questions were missed.

我希望避免使用数据库.它可以在屏幕上,在新屏幕上或通过电子邮件发送.没有偏好.有什么建议吗?

I'd prefer to avoid use of a database. It can be on screen, on a new screen or e-mailed. No preference. Any suggestions?

以下是上述网站的代码:

Here is the code from the above mentioned site:

<form action="grade.php" method="post" id="quiz">
<li>

<h3>CSS Stands for...</h3>

<div>
    <input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
    <label for="question-1-answers-A">A) Computer Styled Sections </label>
</div>

<div>
    <input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
    <label for="question-1-answers-B">B) Cascading Style Sheets</label>
</div>

<div>
    <input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
    <label for="question-1-answers-C">C) Crazy Solid Shapes</label>
</div>

<div>
    <input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
    <label for="question-1-answers-D">D) None of the above</label>
</div>

</li>
</form>
<input type="submit" value="Submit Quiz" />

然后是PHP脚本:

<?php

$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$answer3 = $_POST['question-3-answers'];
$answer4 = $_POST['question-4-answers'];
$answer5 = $_POST['question-5-answers'];

$totalCorrect = 0;

if ($answer1 == "B") { $totalCorrect++; }
if ($answer2 == "A") { $totalCorrect++; }
if ($answer3 == "C") { $totalCorrect++; }
if ($answer4 == "D") { $totalCorrect++; }
if ($answer5) { $totalCorrect++; }

echo "<div id='results'>$totalCorrect / 5 correct</div>";

?>

任何建议或链接将不胜感激.我的Google技能使我失望.我想搜索的所有内容都会带来不相关的内容.

Any suggestions or links would be much appreciated. My Google skills are failing me. Everything I think to search for brings up irrelevant stuff.

推荐答案

要能够回答答案,而不是字母,您需要先存储问题.您不需要使用数据库,只需使用数组即可.

To be able to echo the answer, and not the letter you need to store the question first. You don't need to use a database, you can just use an array.

如果要使用数组,建议将所有内容存储在数组中.由于html的结构是相同的,因此可以节省很多时间.您只需编写一个问题,然后在整个脚本中自动实施该问题即可.

If your going to use arrays, I'd suggest storing everything in an array. Since the structure of the html is just the same, this can save you so much time. You could just write a question once and implement it automatically throughout the script.

<?php 

$Questions = array(
    1 => array(
        'Question' => 'CSS stands for',
        'Answers' => array(
            'A' => 'Computer Styled Sections',
            'B' => 'Cascading Style Sheets',
            'C' => 'Crazy Solid Shapes'
        ),
        'CorrectAnswer' => 'A'
    ),
    2 => array(
        'Question' => 'Second question',
        'Answers' => array(
            'A' => 'First answer of Second question',
            'B' => 'Second answer Second question',
            'C' => 'Third answer Second question'
        ),
        'CorrectAnswer' => 'C'
    )
);

if (isset($_POST['answers'])){
    $Answers = $_POST['answers']; // Get submitted answers.

    // Now this is fun, automated question checking! ;)

    foreach ($Questions as $QuestionNo => $Value){
        // Echo the question
        echo $Value['Question'].'<br />';

        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
            echo '<span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
        } else {
            echo '<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
        }
        echo '<br /><hr>';
    }
} else {
?>
    <form action="grade.php" method="post" id="quiz">
    <?php foreach ($Questions as $QuestionNo => $Value){ ?>
    <li>
        <h3><?php echo $Value['Question']; ?></h3>
        <?php 
            foreach ($Value['Answers'] as $Letter => $Answer){ 
            $Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
        ?>
        <div>
            <input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
            <label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
        </div>
        <?php } ?>
    </li>
    <?php } ?>
    <input type="submit" value="Submit Quiz" />
    </form>
<?php 
}
?>

这很酷的事情是,如果您要添加其他问题,则无需添加任何HTML或任何内容.只需添加问题及其答案,正确答案,它就会自动运行!顺便说一句,这是一个文件,而不是2.因此,它应该提交给自己.

The cool thing about this is that you don't need to add any HTML or anything if you want to add another question. Just add the question with its answers, the correct answer and it automatically works! By the way, this is one file, not 2. So it should submit to itself.

这篇关于屏幕测验中的PHP测验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆