php - 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>
解决方案
session_start()
你在你的文件上加载了吗?如果不是,这可能无法保存您正在设置的会话。您results.php
也可以将代码更改为:
<?php foreach($_SESSION['answers'] as $question => $answer){ ?>
<p>
<?php echo $answer."<br/>"; ?>
</p>
<?php }
?>
希望这可以帮助!
推荐阅读
- javascript - 使用javascript的2个矩形之间的鼠标可拖动碰撞
- powershell - 我需要对 TeamCity API 进行什么 REST 调用才能获得以下构建状态?我用的是不正确的
- git - 如何在 SSH 密钥之间切换以在 github 帐户中使用
- c# - .NET - EDMX/自动生成的 DbContext 在过程导入期间跳过表类型参数
- ios - 在 SwiftUI 中添加圆角半径时如何隐藏背景颜色的溢出?
- apache - 当远程服务器出现 404 错误时禁用 RedirectMatch?
- docker - Docker Compose .env 文件未读取
- css - 文字显示动画
- json - JSON Schema 参考解析
- javascript - 使用 Ajax 启动动态页面似乎会破坏一些 Javascript 功能