douzhi3105 2015-03-11 13:05
浏览 158
已采纳

嵌套while循环php mysql

I have this code:

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

which gives the below output. current

Now my question is how to modify my code (use nested loops?!) so the output is:

modified

Thank you kindly.

NB: Sorry, I do not have enough reputation to attach images. I have uploaded them on an external site.

  • 写回答

1条回答 默认 最新

  • dongpaocuan7498 2015-03-11 13:26
    关注

    It may not be the best solution, but I cannot think of something better right now.
    In the pre-execution I create the grid you want, and in the layout this grid-array is displayed.

    <?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>';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?