I have an application here: Application
The application is there to show you the two drop down menus I want to use as a filter to be able to get Student's Answers determining on the student(s) selected and the question(s) selected form the respective drop down menus.
To see the drop down menus, in the application select an Assessment
from the Assessment
drop down menu and submit, you will see the students and questions drop down menus displayed underneath.
Now what I want to do is to create a dynamic WHERE clause depending on the options selected from the student(s) and question(s) drop down menus when the user clicked on the Get Student Answers
button.
Below is the current query. The query has to have the default clause SessionId = ?
at all times in the WHERE clause. The other clauses studentId = ?
and questionId = ?
depends on the options the user
has chose from both drop downs.
$selectedstudentanswerqry = "
SELECT
StudentAlias, q.SessionId, QuestionNo, QuestionContent, o.OptionType, GROUP_CONCAT( DISTINCT Answer
ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType,
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime
FROM Student s
INNER JOIN Student_Answer sa ON (s.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
WHERE (SessionId = ?)
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
// You only need to call bind_param once
$selectedstudentanswerstmt->bind_param("i",$_POST["session"]);
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result($detailsStudentAlias,$detailsSessionId,$detailsQuestionNo,
$detailsQuestonContent,$detailsOptionType,$detailsAnswer,$detailsReplyType,$detailsStudentAnswer,$detailsResponseTime);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
Below is the Students and Questions drop down menus as a sample html:
Students Drop Down Menu:
<select name="student" id="studentsDrop">
<option value="All">All</option>
<option value="3">u0499220 - Jack Briggs</option>
<option value="7">u0093220 - Mary Kay</option>
</form>
Questions Drop Down Menu:
<select name="question" id="questionsDrop">
<option value="All">All</option>
<option value="34">1</option>
<option value="35">2</option>
<option value="36">3</option>
</form>
I am thinking of something like if a specific student is selected then include StudentId = ?
in WHERE clause, if specific question number is selected then include QuestionId = ?
in WHERE clause. But if the All
value is selected in Student drop down menu then remove StudentId = ?
from the WHERE clause as we are look for all student, not whittling down for a specific student. This is same for if All
value is selected from Question drop down menu but obviously dealing with QuestionId = ?