It is necessary to build on the basis of two SQL tables one common table Statement. The first sets the number and names of columns with disciplines. The second stores data about students and their assessments. Each evaluation record by discipline is unique. The problem is to display the records of the second table in the required format, so that in the disciplines for which the assessment is given the value of the assessment is derived, and for the disciplines that are not set, nothing.

For clarity Statement

Tell me how to organize the output so that at these positions instead of Array the values ​​of $ columns [$ i] [$ j] ['record_id'] and $ columns [$ i] [$ j] ['record_mark'] are output?

Code:

for ($i=0; $i < $studentsLength; $i++) { $columns[$i] = []; for ($k=0; $k < $recordsLength; $k++) { for ($j=0; $j < $disciplinesLength; $j++) { if ($records[$k]->Discipline_ID == $disciplines[$j]->id && $records[$k]->Student == $students[$i]->StudentName) { $columns[$i][$j]['record_id'] = $records[$k]->id; $columns[$i][$j]['record_mark'] = $records[$k]->Mark; } } } print "<tr> <td>{$students[$i]->StudentName}</td>"; for ($l=0; $l < $disciplinesLength; $l++) { //print_r($columns[$i]); if (in_array($l, $columns[$i])) { print "<td>{$columns[$i][$l]}</td>"; } else { print "<td></td>"; } } print "</tr>"; 
  • And where does sql. in the code, only work with php arrays that has nothing to do with the database - Mike
  • And once there is an inscription array, then it is probably assumed that there may be several values. If there is one value, then you just need to know in which element of the array it lies and output this particular element - Mike
  • @Mike yes, id and grades should be stored in the column cell. But no matter what I do, I can’t get what I’m trying to display, it’s trite to put the necessary values ​​in $ columns [$ i] [$ j] ..... - SNuSNuMR1996

1 answer 1

The problem is solved by checking for the existence of a value in the required column using the isset () function in the last loop.

 for ($l=0; $l < $disciplinesLength; $l++) { if (isset($columns[$i][$l])) { print "<td>{$columns[$i][$l]['record_id']}{$columns[$i][$l]['record_mark']}</td>"; } else { print "<td></td>"; } }