Your cases:
- Student selected != 'All' : Add StudentId = ? in WHERE clause
- Student selected == 'All' : Remove StudentId = ? from WHERE clause
- Question selected != 'All' : Add QuestionId = ? in WHERE clause
- Question selected == 'All' : Remove QuestionId = ? from WHERE clause
<?php
$selectedstudentanswerqry = "WHERE SessionId = ? ";
if ($_POST['student'] != 'All'){
$selectedstudentanswerqry .= " and StudentId = ? ";
}
else{
/*
$selectedstudentanswerqry .= "
//what is condition for if student == all ?
";
*/
}
if ($_POST['question'] != 'All'){
$selectedstudentanswerqry .= " and QuestionId = ? ";
}
else{
}
?>
consider that
student = 1 then:
if student != All = true
if question != AA = true
student = All
if student != All = false
if question != AA = true
question = 1
if student != All = true
if question != AA = true
question = All
if student != All = true
if question != AA = false
check if you want this? I think no.
//case1
if ($_POST['student'] != 'All'){
$selectedstudentanswerqry .= "
WHERE (SessionId = ? AND StudentId = ?)
";
}
//case2
if ($_POST['question'] != 'All'){
//case 2.1
if ($_POST['student'] != 'All'){
$selectedstudentanswerqry .= "
and (QuestionId = ?)
";
}
//case 2.2
else{
$selectedstudentanswerqry .= "
WHERE (SessionId = ? AND QuestionId = ?)
";
}
}
/*
testing
1- student != All, question != All
case1: true
case1: result: $selectedstudentanswerqry = WHERE (SessionId = ? AND StudentId = ?)
case2 : true
case 2.1: true
case2.1 result: $selectedstudentanswerqry .= and (QuestionId = ?)
2- student != All question = All
case1: true
case1: result: $selectedstudentanswerqry = WHERE (SessionId = ? AND StudentId = ?)
case2: false
3- student = All question != All
case1: false
case2: true
case2.1: false
case2.2: true
case2.2 result: $selectedstudentanswerqry = WHERE (SessionId = ? AND QuestionId = ?)
4- student = All question = All
case1: false
case2: false
*/