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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题