如果用户选择“全部”选项,如何显示所有详细信息

时间:2013-01-30 17:46:17

标签: php mysqli

下面我有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。 / p>

但问题是,如果我从下拉菜单中选择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

CODE UPDATED:

    $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]);
}

截图:

enter image description here

如上所示,它显示我选择了学生,但我选择了All Qustions。但它只在Student Answer标题下显示一个问题,在此示例中,您可以在括号中看到的问题总数为2,因此它应显示2个问题,而不是一个问题

DB SCREENSHOT:

稍微缩短了普通查询的版本,但这不会影响主查询,因为它也显示相同的行数。它显示2行,因为有两个问题。以下结果适用于单个学生StudentId = 1,评估26中的所有问题(SessionId = 26)。

enter image description here

2 个答案:

答案 0 :(得分:2)

如果值可以是全部或“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

答案 1 :(得分:1)

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

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

您似乎要做的就是更改选择中“全部”的,并为 Session 添加一些代码。我添加了一个“问题”以解决问题。

  • 更改“全部”的所有选项值,设置为“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 Selects ,当选择All时,它们的值为“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]);
    }
}

edit2

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

删除array s:

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