PHP测验,屏幕上显示结果

4
我正在尝试创建一个简单的测验,基于我找到的教程。http://css-tricks.com/building-a-simple-quiz/ 不幸的是,这让我一筹莫展,答案可能很简单。
我已经完美地解决了这个问题。我想改变功能。而不是计算答案,我想做其他事情。
  1. 我想再次显示问题。
  2. 此外,选择的答案和正确答案。回显答案,不是字母A、B、C、D。
对我来说,有一个测验,说你错过了2个,但没有显示哪些问题被错过,这似乎很傻。 我希望避免使用数据库。可以在屏幕上、新屏幕上或通过电子邮件发送。没有偏好。有什么建议吗?
这是上述网站的代码:
<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>";

?>

非常感谢您的提问。我很乐意帮助您翻译以下内容:“任何建议或链接都将不胜感激。我的谷歌搜索技能正在失败。我想搜索的所有内容都会带来无关的东西。”

你需要找到一种方法,在 index.php 测验文件中将 grade.php 中的函数整合进去,并将表单操作设置为 $_SERVER['PHP_SELF'];,或者在其中某个地方(例如 DIV 中)设置一个 include file 来包含测验问题。除此之外,这需要一些相当高级的编码技巧。我可能能够完成,但需要一些时间。不过这是个好问题。 - Funk Forty Niner
4个回答

10

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

如果您要使用数组,我建议将所有内容都存储在数组中。由于HTML的结构完全相同,这可以节省大量时间。您只需编写一次问题并自动实现整个脚本。

<?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个文件。所以它应该提交到自己。


1
谢谢Gillian,很好用。将表单操作从“grade.php”更改为“<?php echo $_SERVER['PHP_SELF']; ?>”。 “C B” - Funk Forty Niner
1
Gillian,这是一个很棒的解决方案。我采纳了Fred的建议,效果很好。我对其中一部分很好奇。在if else中,它返回的结果是不正确的(红色)。是否可以让它同时返回正确的答案(无论黑色或绿色都可以)? - C B
在foreach循环中,正确答案存储在$Value ['CorrectAnswer']中。用户提供的答案存储在$Answers [$QuestionNo]中。谢谢。 - Gilly
使用大写字母来命名变量和数组时,不是很容易混淆吗? - Kard Nails
@KardNails 你说得对。这可能会让人感到困惑。我想我两年前还是个新手 :) - Gilly

2
基本结构应该是这样的:
if ($answer1 == "B") { 
   $totalCorrect++;
} else {
   $wronganswers[] = "You got #1 wrong. correct answer is B / ...text_of_answer_here ";
}

...

if ($totalCorrect != $number_of_questions) {
    echo implode($wronganswers);
}

0

关于网页,这里有一个简单的经验法则:

HTML - 内容

CSS - 样式

JavaScript - 行为

只需在其中添加一些非常简单的JS,而不是仅仅使用$totalcorrect,它就可以实时更新。

PHP是服务器端的,仅在服务器上运行一次(并将您的网页输出到客户端)。 JS是客户端的,只要您告诉它*在客户端上运行多长时间,它就会一直运行。

(*一般意思,不完全正确,但是功能正常)

编辑:如果您正在遵循PHP教程,则此方法无法帮助


0
试试这个:我修改了Gillian lo wong的代码,在结尾处添加了一个分数,并显示您的错误答案。
<?php 

    $Questions = array(
        1 => array(
            'Question' => '1. CSS stands for',
            'Answers' => array(
                'A' => 'Computer Styled Sections',
                'B' => 'Cascading Style Sheets',
                'C' => 'Crazy Solid Shapes'
            ),
            'CorrectAnswer' => 'B'
        ),
        2 => array(
            'Question' => '2. What is the Capital of the Philippines',
            'Answers' => array(
                'A' => 'Cebu City',
                'B' => 'Davao City',
                'C' => 'Manila City'
            ),
            '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 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
                 echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
            } else {
                echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
                echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; $counter++;

            }

            echo '<br /><hr>'; 
                                    if ($counter=="") 
                                    { 
                                    $counter='0';
                                    $results = "Your score: $counter/2"; 
                                    }
                                    else 
                                    { 
                                    $results = "Your score: $counter/2"; 
                                    }
                }                           echo $results;
    } else {  
    ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
        <?php foreach ($Questions as $QuestionNo => $Value){ ?>

            <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 } ?>

        <?php } ?>
        <input type="submit" value="Submit Quiz" />
        </form>
    <?php 
    }
    ?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接