嵌套 while 循环 php mysql

nested while loops 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 
    }
?> 

给出以下输出。

现在我的问题是如何修改我的代码(使用嵌套循环?!)所以输出是:

谢谢你。

注意:抱歉,我没有足够的信誉来附加图像。我已将它们上传到外部网站。

这可能不是最好的解决方案,但我现在想不出更好的办法。
在执行前我创建了你想要的网格,在布局中显示了这个网格数组。

<?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>';
?>