下面我有 2 个 HTML 下拉菜单,一个用于学生,另一个用于问题:
<select name="question" id="questionsDrop">
<option value="0">All</option>
<option value="2">What is 2+2</option>
<option value="34">What is 3+3</option>
<option value="42">What is 4+4</option>
<option value="51">What is 5+5/option>
</select>
<select name="student" id="studentsDrop">
<option value="0">All</option>
<option value="23">Jay Hart</option>
<option value="32">Bubba Wright</option>
<option value="43">Tim Grey</option>
<option value="52">Mary Pine</option>
</select>
下面是一个 mysqli 查询,它将根据从上面两个下拉菜单中选择的选项输出结果:
$selectedstudentanswerqry = "
SELECT
sa.StudentId, StudentAlias, StudentForename, StudentSurname, q.SessionId,
QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers,
GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks,
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
FROM Student st
INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
";
// Initially empty
$where = array('q.SessionId = ?');
$parameters = array($_POST["session"]);
$parameterTypes = 'i';
// Check whether a specific student was selected
if($_POST["student"] != '0') {
$where[] = 'sa.StudentId = ?';
$parameters[] .= $_POST["student"];
$parameterTypes .= 'i';
}
// Check whether a specific question was selected
// NB: This is not an else if!
if($_POST["question"] != '0') {
$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, QuestionNo
";
.......................................................................................
$arrStudentId = array();
$arrStudentAlias = array();
$arrStudentForename = array();
$arrStudentSurname = array();
$arrQuestionNo = array();
$arrQuestionContent = array();
while ($selectedstudentanswerstmt->fetch()) {
$arrStudentId[ $detailsStudentId ] = $detailsStudentId;
$arrStudentAlias[ $detailsStudentId ] = $detailsStudentAlias;
$arrStudentForename[ $detailsStudentId ] = $detailsStudentForename;
$arrStudentSurname[ $detailsStudentId ] = $detailsStudentSurname;
$arrQuestionNo[ $detailsStudentId ] = $detailsQuestionNo;
$arrQuestionContent[ $detailsStudentId ] = $detailsQuestonContent;
}
foreach ($arrStudentId as $key=>$student) {
echo '<p><strong>Question:</strong> ' .htmlspecialchars($arrQuestionNo[$key]). ': ' .htmlspecialchars($arrQuestionContent[$key]). '</p>' . PHP_EOL;
}
现在,如果我从相应的下拉菜单中选择特定学生或特定问题,则详细信息输出没有问题,因为数据库能够从这些下拉菜单中检索值,因为它们是数据库中的实际 ID。
但问题是,如果我All
从任一下拉菜单中选择一个选项。All
两个下拉菜单中的选项值都是0
。现在0
不在数据库中作为 id,我想做的是,如果用户选择该All
选项,则All
如果在学生下拉菜单中选择,则显示学生详细信息, All
如果在问题下拉菜单中选择,则显示问题详细信息。
现在,查询正在收集数据以执行此操作,因为它使用为包含适当条件而构建的动态 WHERE 子句。All
但我的问题是,如果用户选择了相关下拉菜单中的选项,我怎样才能让它显示学生/问题的所有详细信息?
更新处理 RING0 答案:
Notice: Array to string conversion in ... on line 358
Warning: mysqli::prepare(): (42S22/1054): Unknown column 'Array' in 'where clause' in ... on line 360
Fatal error: Call to a member function bind_param() on a non-object in ... on line 370
代码更新:
$selectedstudentanswerqry = "
SELECT
sa.StudentId, StudentAlias, StudentForename, StudentSurname, q.SessionId,
QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers,
GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks,
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
FROM Student st
INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
";
// Initially empty
$where = array();
$parameters = array();
$parameterTypes = '';
// Check whether a specific session was selected
if($_POST["session"] != '0') {
$where[] = array('q.SessionId = ?');
$parameters[] = array($_POST["session"]);
$parameterTypes .= 'i';
}
// Check whether a specific student was selected
if($_POST["student"] != '0') {
$where[] = 'sa.StudentId = ?';
$parameters[] .= $_POST["student"];
$parameterTypes .= 'i';
}
// Check whether a specific question was selected
// NB: This is not an else if!
if($_POST["question"] != '0') {
$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(count($where) > 0) {
$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]);
}
截屏:
正如您在上面看到的,它显示我选择了学生,但我选择All
了 Qustions。然而它只在Student Answer
标题下显示一个问题,在这个例子中,你可以在括号中看到的问题总数是2
,所以它应该显示 2 个问题,而不是一个问题
数据库截图:
略微缩短的普通查询版本,但这不会影响主查询,因为它也显示相同数量的行。它显示 2 行,因为有两个问题。下面的结果是针对单个学生StudentId = 1
,评估 26 ( SessionId = 26
) 中的所有问题。