php - 嵌套的while循环php mysql

标签 php mysql nested-loops

我有这个代码:

<?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 
    }
?> 

给出以下输出。 current

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

modified

谢谢你。

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

最佳答案

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

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

关于php - 嵌套的while循环php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28987389/

相关文章:

php - 在 Yii Framework 中处理输出

php - 如何填充下拉菜单

javascript - 正则表达式:嵌套标签

javascript - Uncaught ReferenceError : $this is not defined ajax

javascript - javascript中第一个函数中的setTimeout()和第二个函数中的闭包是否会出现堆栈溢出错误?

java - 如何在循环之外使用在循环中找到值的变量?

php - 如何在 PHP 中访问/获取通过 iPhone 应用程序上传的文件的数据?

php - ActiveCollab 2.3.1 : Using SVN integration

MySql Insert ON Duplicate Key 并选择不起作用

python MySQLdb不会插入到数据库中