I made a form to show the correct answers for the students for any given week.
A student can enter his or her course, student number and week number and see a table with 3 columns, question number, correct answer, student’s answer.
Works OK, but, when the student did not hand in any answers for the week given, it crashes, blank screen, because that combination of student number and week number is not in the table.
I don’t really understand $indexresult
Has it already checked the table for results? Or does that happen in the following foreach loop?
How can I tell if it is empty, no results for that week number and student number??
if(isset($_POST['weeknr'])){
$indexsql = 'SELECT id FROM allstudentsAnswers' . $class . 'CW WHERE studentnr = \'' . $studentnr . '\' AND weeknr = \'' . $weeknr . '\'';
$indexresult = $pdo->query($indexsql);
//$indexresult is: PDOStatement Object ( [queryString] => SELECT id FROM allstudentsAnswers20BECW WHERE studentnr = '1234567890' AND weeknr = 'Week4' )
// if there are no results go back to form.html.php
If the combination of student number and week number is in the table, no problem, first get the id of the rows, later get the answers in those rows (students may send more than once):
foreach ($indexresult as $row){
$indices[] = $row;
}
For now, added this if clause after the foreach loop:
if(!$indices) {
$_SESSION[‘error’] = 'No results for this student number: ’ . $studentnr . ’ and week number: ’ . $weeknr . ‘’;
include ‘form.html.php’;
exit();
}
Is this the best way to do this?