如果我从下拉菜单中选择一个学生,这是很奇怪的(如果$_POST['student'] != 0
为值0
代表一个All
选项而所有其他选项都有自己的选项数字`),我收到一个错误说明:
Notice: Undefined variable: questions in ... on line 597 Warning: Invalid argument supplied for foreach() in ... on line 597
我的问题是导致此错误的原因以及如何解决?
代码低于我试图尽可能减少的代码,在代码片段的底部附近是错误行被注释的地方:
$selectedstudentanswerqry = "
SELECT
sa.StudentId, StudentAlias, StudentForename, StudentSurname
...
FROM Student st
...
";
// Initially empty
$where = array('q.SessionId = ?');
$parameters = array($_POST["session"]);
$parameterTypes = 'i';
//check if POST is empty
// Check whether a specific student was selected
$p_student = empty($_POST["student"])?0:$_POST["student"]; // Now if $_POST['student'] is either 0 or empty $p_student will be 0
switch($p_student){
case 0:
//dont' add where filters
break;
default:
$where[] = 'sa.StudentId = ?';
$parameters[] .= $_POST["student"];
$parameterTypes .= 'i';
}
// Check whether a specific question was selected
$p_question = empty($_POST["question"])?0:$_POST["question"]; // Same here, if $_POST['question'] is either 0 or empty $p_question will be 0
switch($p_question){
case 0:
//dont' add where filters
break;
default:
$where[] = 'q.QuestionId = ?';
$parameters[] .= $_POST["question"];
$parameterTypes .= 'i';
}
// If we added to $where in any of the conditionals, we need a WHERE clause in
// our query
if(!empty($where)) {
$selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
// You only need to call bind_param once
}
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
if (count($where) == 1) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
}
else if (count($where) == 2) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
}
else if (count($where) == 3) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1], $parameters[2]);
}
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result($detailsStudentId,$detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
$arrStudentId = array();
$arrStudentAlias = array();
...
while ($selectedstudentanswerstmt->fetch()) {
//Check if the student data exist.
if (!isset($questions[$detailsStudentId])) {
$questions[$detailsStudentId] = array(
'studentalias' => $detailsStudentAlias,
'studentforename' => $detailsStudentForename,
'studentsurname' => $detailsStudentSurname,
'questions' => array()
);
}
$questions[$detailsStudentId]['questions'][$detailsQuestionId] = array(
'questionno'=>$detailsQuestionNo,
'content'=>$detailsQuestionContent,
.....
);
}
$selectedstudentanswerstmt->close();
?>
//LINE 597 ERROR BELOW
foreach ($questions as $studentId => $studentData) {
echo '<hr><h3>'.$studentData['studentalias'].' - '.$studentData['studentforename'].' '.$studentData['studentsurname'].'</h3>';
foreach ($studentData['questions'] as $questionId => $questionData) {
echo '<h3>'.$questionData['questionno'].': '.$questionData['content'].'</h3>';
}
}
答案 0 :(得分:0)
变量$ questions本身的初始化可能是问题所在。查看代码并查看调用$questions =
之类的内容。可能每次都在一个不运行if语句中。
如果你打电话:
$questions[...]
并且从未将$questions
初始化为数组,您也会遇到问题。
因此,您需要使用以下内容初始化$questions
$questions = array();
在此之后,您将无法访问数组的字段。