вложенные циклы while php mysql

У меня есть этот код:

<?php

if( isset($_POST['groups'])){

$groups = $_POST['groups'];
$subject = $_POST['subject'];

$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno

FROM    table_1 a, table_2 b

WHERE   b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";

$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); ?>

<table width="100%" border="1" cellspacing="0" cellpadding="3" >
<tr>
<td align="center"><strong><font size="2">Students</font></strong></td>
<td align="center"><strong><font size="2">Date</font></strong></td>
<td align="center"><strong><font size="2">Attendance</font></strong>    </td>
</tr>

<?php
while($rows=mysqli_fetch_array($result)){

$date = $rows['date']; $date2 = date("d-F-Y", strtotime($date));

$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";
?>

<tr>
<td align="center"><font size="2"><?php echo $rows['groupstudents']; ?></font>    </td>
<td align="center"><strong><font size="2"><?php echo $date2; ?></font></strong>    </td>
<td align="center"><font size="2"><?php echo $class1; ?></font></td>
</tr>

<?php
}
?>

что дает ниже вывод.
ток

Теперь мой вопрос заключается в том, как изменить мой код (использовать вложенные циклы ?!), чтобы получить следующий результат:

модифицированный

Спасибо, любезно.

NB: Извините, у меня недостаточно репутации, чтобы прикреплять изображения. Я загрузил их на внешний сайт.

1

Решение

Возможно, это не лучшее решение, но я не могу сейчас придумать что-то лучшее.
В предварительном исполнении я создаю нужную вам сетку, и в макете отображается этот сеточный массив.

<?php

if( isset($_POST['groups'])){

$groups = $_POST['groups'];
$subject = $_POST['subject'];

$sql="SELECT
a.groupcode, a.groupstudents, a.studentid,
b.groupcode, b.coursename, b.studentid, b.date, b.class1, b.attend, b.attendno

FROM    table_1 a, table_2 b

WHERE   b.groupcode = '$groups' AND b.coursename = '$subject' AND
(a.studentid = b.studentid AND a.groupcode = b.groupcode)";

$result=mysqli_query($GLOBALS["___mysqli_ston"], $sql);

$dates = array();
$display = array();

while ($rows=mysqli_fetch_array($result)) {
if (!isset($display[$rows['groupstudents']])) {
$display[$rows['groupstudents']] = array();
}

if (!isset($dates[strtotime($rows['date'])])) {
$dates[strtotime($rows['date'])] = count($dates);
}

$class1 = $rows['class1'];
if ($class1 == 0) $class1 = "No Class"; if ($class1 == 1) $class1 = "Absent";
if ($class1 == 3) $class1 = "Present"; if ($class1 == 2) $class1 = "Late";

$display[$rows['groupstudents']][$dates[strtotime($rows['date'])]] = $class1;
}

echo '<table width="100%" border="1" cellspacing="0" cellpadding="3">';
echo '<tr>';
echo '<td align="center"><strong><font size="2">Students</font></strong></td>';
foreach ($dates as $date => $reversedIndex) {
echo '<td align="center"><strong><font size="2">' . date("d-F-Y", $date) . '</font></strong></td>';
}
echo '</tr>';

foreach ($display as $student => $row) {
echo '<tr>';
echo '<td align="center"><font size="2">' .  $student . '</font></td>';

foreach ($dates as $date => $index) {
echo '<td align="center"><font size="2">';
if (isset($row[$index])) {
echo $row[$index];
} else {
echo '';
}
echo '</font></td>';
}
echo '</tr>';
}

echo '</table>';
?>
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector