選択したドロップダウンメニューに応じて結果を出力する動的where句を使用したクエリが以下にあります。これで、選択したオプションに応じて特定の条件を追加および削除する際にwhere句が正しく機能するようになりました。
しかし、私が気付いたのは、すべての質問を見たい場合、それQuestionId = ?
は良い状態ではないということですが、各質問の回答を次のように表示するのではなく、表示することになっている場合:
D (this is for question 1)
B,C,E (this is for question 2)
以下のように、それらすべてを1つの回答として表示していますが、これは正しくありません。
D,B,C,E
私の質問は、これ、クエリ、またはループの原因は何ですか?
var_dump($ questions):これを表示します:
array(1) { [39]=> array(1) { [72]=> array(1) { ["answer"]=> string(7) "B,C,D,E" } } }
72はQuestionIdであるため、奇妙な理由ですべての質問を通過しているわけではありません(73である2番目の質問があるはずです)
以下はコードです:
$selectedstudentanswerqry = "
SELECT sa.StudentId, q.QuestionId,
GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer
FROM Student st
INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
";
// 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"])?'':$_POST["student"];
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"])?'':$_POST["question"];
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);
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
// You only need to call bind_param once
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]);
}
}
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId
";
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result($detailsStudentId,$detailsQuestionId,$detailsAnswer);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
$questions = array();
while ($selectedstudentanswerstmt->fetch()) {
$questions[$detailsStudentId][$detailsQuestionId] = array(
'answer'=>$detailsAnswer,
);
}
...
<?php
var_dump($questions);
foreach ($questions as $studentId => $student)
{
foreach ($student as $questionId => $question) {
echo '<p><strong>Answer:</strong> ' .htmlspecialchars($question['answer']). '</p>' . PHP_EOL;
}
}
?>