0

下面我有 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) 中的所有问题。

在此处输入图像描述

4

2 回答 2

2

如果该值既可以是 All 也可以是“0”,您可以进行切换

<?php

//check if POST is empty

$p_student = empty($_POST["student"])?'':$_POST["student"];

switch($p_student){
case 'All':
case '0':
    //dont' add where filters
    break;
default:
    $where[] = 'sa.StudentId = ?';
    $parameters[] .= $_POST["student"];
    $parameterTypes .= 'i';
}

$p_question = empty($_POST["question"])?'':$_POST["question"];

switch($p_question){
case 'All':
case '0':
    //dont' add where filters
    break;
default:
    $where[] = 'q.questionId = ?';
    $parameters[] .= $_POST["question"];
    $parameterTypes .= 'i';
}

请记住,有时在以后直接访问或刷新页面时它是一个GET 。

所以也许你需要一个$_REQUEST ['student']

放置了整个代码,我的答案合并在 https://github.com/fedmich/StackOverflow-answers/blob/master/14610396/index.php

于 2013-02-04T02:32:32.953 回答
1

查看您提供的代码,我注意到以下内容:

  • 选项中的“全部”值设置为“全部”,而应为“0”
  • 会话是可选择的,而 PHP 代码不检查是否已选择会话
  • 您显示的 HTML 代码中没有问题select

看来您所要做的就是更改Selects中“All”的值,并为Session添加一些代码。我添加了一个“问题”来解决这个问题。

  • 更改“All”的所有选项值,设置为“0”:

代码:

<select name="session" id="sessionsDrop">
<option value="0">All</option>
<option value="2">EOWOW</option>
<option value="34">EOWOW</option>
<option value="42">EEMOO</option>
<option value="51">EDOOS</option>
</select>  


<select name="student" id="studentsDrop">
<option value="0">All</option>
<option value="23">Jay Hart</option>
...

以及未出现在 HTML 代码中的“问题”

<select name="question" id="questionDrop">
<option value="0">All</option>
<option value="1">What is the 50th State?</option>
...
  • 添加会话的测试代码

向空数组添加一些代码,并检查是否选择了会话

// Initially empty
$where = array();
$parameters = array();
$parameterTypes = '';

// Check whether a specific session was selected
if($_POST["session"] != '0') {
  $where[] = 'q.SessionId = ?';
  $parameters[] = $_POST["session"];
  $parameterTypes .= 'i';
}

// then same code

// Check whether a specific student was selected
if($_POST["student"] != '0') {
    $where[] = 'sa.StudentId = ?';
    $parameters[] .= $_POST["student"];
    $parameterTypes .= 'i';
}
...

以这种方式在PHP代码中检查3所选择,当“选择”时,它们的值为“0”。

代码应该在哪里

if(count($where) > 0) {
    global $mysqli;

    $selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
    $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]);
    }
}

编辑2

if($_POST["session"] != '0') {
  $where[] = array('q.SessionId = ?');
  $parameters[] = array($_POST["session"]);

删除arrays:

if($_POST["session"] != '0') {
  $where[] = 'q.SessionId = ?';
  $parameters[] = $_POST["session"];
于 2013-02-04T01:59:00.893 回答