预定字符串后的打印条件

时间:2014-03-15 23:58:32

标签: php html printing

我一直在试图弄清楚这一点30分钟,似乎不能让它适用于多行。我正在尝试打印行号后跟一个空格,然后插入测试结果,然后换行。这是php文件:

<!DOCTYPE html>
<html>
    <head>
        <title>Quiz Results</title>
        <link rel="stylesheet" type="text/css" href="http://localhost/webtech/sample.css" />
    </head>
    <body>
        <h2>Quiz results</h2>
        <p>Here are the results of the quiz:</p>
    <?php
        $choice0 = $_POST['choice0'];
        $choice1 = $_POST['choice1'];
        $choice2 = $_POST['choice2'];
        $choice3 = $_POST['choice3'];
        $quizfile = fopen('quizfile.txt', 'r');
        $quizans = fgets($quizfile);
        fclose($quizans);
        list($ans0, $ans1, $ans2, $ans3) = explode(':', $quizans);
        if ($choice0 == $ans0)
        {
            echo '1. You selected the right anwser!';
        } else {
            echo '1. Incorrect. The right anwser is '.$ans0.'.';
        }
        if ($choice1 == $ans1)
        {
            echo '</br>2. You selected the right answer!';
        } else {
            echo '</br>2. Incorrect. The right anwser is '.$ans1.'.';
        }
        if ($choice2 == $ans2)
        {
            echo '</br>3. You selected the right anwser!';
        } else {
            echo '</br>3. Incorrect. The right anwser '.$ans2.'.';
        }
        if ($choice3 == $ans3)
        {
            echo '</br>4. You selected the right anwser!';
        } else {
            echo '</br>4. Incorrect. The right anwser is '.$ans3.'.';
        }
        ?>
        <p><a href="http://localhost/webtech/coursework/chapt7/quiz.html">Redo Quiz</a>
        <a href="http://localhost/index.html">Go back to Homepage</a></p>
    </body>
</html>

这是html:

<!DOCTYPE html>
<html>
    <head>
        <title>Quiz Results</title>
        <link rel="stylesheet" type="text/css" href="http://localhost/webtech/sample.css" />
    </head>
    <body>
        <h2>Quiz results</h2>
        <p>Here are the results of the quiz:</p>
    <?php
        $choice0 = $_POST['choice0'];
        $choice1 = $_POST['choice1'];
        $choice2 = $_POST['choice2'];
        $choice3 = $_POST['choice3'];
        $quizfile = fopen('quizfile.txt', 'r');
        $quizans = fgets($quizfile);
        fclose($quizans);
        list($ans0, $ans1, $ans2, $ans3) = explode(':', $quizans);
        if ($choice0 == $ans0)
        {
            echo '1. You selected the right anwser!';
        } else {
            echo '1. Incorrect. The right anwser is '.$ans0.'.';
        }
        if ($choice1 == $ans1)
        {
            echo '</br>2. You selected the right answer!';
        } else {
            echo '</br>2. Incorrect. The right anwser is '.$ans1.'.';
        }
        if ($choice2 == $ans2)
        {
            echo '</br>3. You selected the right anwser!';
        } else {
            echo '</br>3. Incorrect. The right anwser '.$ans2.'.';
        }
        if ($choice3 == $ans3)
        {
            echo '</br>4. You selected the right anwser!';
        } else {
            echo '</br>4. Incorrect. The right anwser is '.$ans3.'.';
        }
        ?>
        {<p><a href="http://localhost/webtech/coursework/chapt7/quiz.html">Redo Quiz</a>
        <a href="http://localhost/index.html">Go back to Homepage</a></p>}
    </body>
</html>

Thx伙计们!

1 个答案:

答案 0 :(得分:0)

我不完全确定你的目标是什么......但我已经在这里清理了你的代码..希望这会有所帮助。

/*
$choice0 = $_POST['choice0'];
$choice1 = $_POST['choice1'];
$choice2 = $_POST['choice2'];
$choice3 = $_POST['choice3'];
list($ans0, $ans1, $ans2, $ans3) = explode(':', $quizans);
*/

$quizfile = fopen('quizfile.txt', 'r');
$quizans = fgets($quizfile);
fclose($quizans);

$answers = array();
$answers = explode(':', $quizans);

foreach($answers as $key => $answer) {

    $choice = $_POST['choice' . $key] ? $_POST['choice' . $key] : NULL;

    if($choice)
    {
        if ($choice == $answer)
        {
            echo $key + 1 . '. You selected the right anwser!' . '<br>';
        } 
        else 
        {
            echo $key + 1 . '. Incorrect. The right anwser is '.$answer.'.' . '<br>';
        }
    }

}