首页 > 解决方案 > PHP ...使用forloop显示数组(调查结果)

问题描述

我是 PHP 新手,正在处理一项任务。我快完成了,但我觉得这最后一步是在用头撞墙。

用户选择一项调查,然后回答 10 个问题。回答第 10 个问题后,他们将被重定向到 results.php 页面,该页面显示他们的问题和答案。

我知道我需要使用foreach循环,但这是我第一次使用它们。我已经完成了多个站点以了解它们的工作方式,但这些示例并不能帮助我理解如何使其在我的作业中发挥作用。

现在我在运行文件时得到这个:

解析错误:语法错误,意外的 'endforeach' (T_ENDFOREACH),预计第 38 行 C:\xampp\htdocs\sandbox\HW4\results.php 中的文件结尾

当我删除 endforeach 时,我被告知 echo 中的 $answer 是未定义的。

我已经没有办法改变我的循环了。没有任何效果。一些帮助将不胜感激。如果我包含与我的问题无关的代码,我深表歉意。

由于对 PHP 了解甚少,我不太确定我需要包含哪些内容以及不需要包含哪些内容。我正在包括我的survey.php 文件中的代码,其中包括数组会话和我的结果文件中的代码,该文件应该包含我的foreach循环。

感谢您的时间。

$isPostBack = filter_input(INPUT_POST, 'submitButton') !== NULL;
if ($isPostBack) {
    // This is what happens if there is a postback.
    $choose_survey = filter_input(INPUT_POST, 'choose_survey', FILTER_VALIDATE_INT);
    if ($choose_survey !== NULL) {
        // Check the value of $choose_survey and then set 'survey' accordingly, e.g.
        // These numbers are the keys to the arrays with the list of surveys
        switch ($choose_survey) {
            case 0:
                $_SESSION['survey'] = $survey0;
                break;
            case 1:
                $_SESSION['survey'] = $survey1;
                break;
            case 2:
                $_SESSION['survey'] = $survey2;
                break;
            default:
                break;
        }

        $_SESSION['answers'] = array();
        $_SESSION['number'] = 1;
    } else {
        // A survey is not selected because it was already chosen.
        // get the value from the radio button.
        $answer = filter_input(INPUT_POST, 'answer', FILTER_DEFAULT);
        if ($answer == NULL) {
            echo '<p>Please select an answer before clicking Submit.</p>';
        } else {
            $question_key = filter_input(INPUT_POST, 'question_key', FILTER_VALIDATE_INT);
            $question = $_SESSION['survey'][$question_key];
            // this will be used later to display the answers/results
            $_SESSION['answers'][$question] = $answer;
            unset($_SESSION['survey'][$question_key]);
            // This is adding 1 to the question number unless session  number is 10
            if ($_SESSION['number'] == 10) { // if = to 10 then we redirect to next page.
                header('Location: results.php');
            } else { // If number is less than 10, we add 1
                $_SESSION['number'] += 1;
            }
        }
    }
} else {
    // This is what happens if there is no postback.
}
?>

结果.php 文件

<br /> <p>Thank you for participating in the survey!</p>
<html lang="en">
<head>
    <title>Survey Results</title>
    <link rel="stylesheet" type="text/css" href="">
</head>
<body>
    <form action="logout.php">
        <p>Here are your results</p>
        <input type="submit" id="submit" value="Logout" />
    </form>
        <section>
            <?php foreach ($_SESSION['answers'] as $question => $answer) ?>
            <p>
                <?php echo "$answer <br/>"; ?>
            </p>
         <?php endforeach; ?>
        </section>
</body>

标签: php

解决方案


session_start()你在你的文件上加载了吗?如果不是,这可能无法保存您正在设置的会话。您results.php也可以将代码更改为:

<?php foreach($_SESSION['answers'] as $question => $answer){ ?>
        <p>
            <?php echo $answer."<br/>"; ?>
        </p>
     <?php }
  ?>

希望这可以帮助!


推荐阅读