如何使用php按学年和学期显示表格?

如何使用php按学年和学期显示表格?,php,mysql,Php,Mysql,我正在尝试使用PHP按学年显示一个表。我觉得它似乎不是很好,因为学年显示冗余 如何简单地像这样显示它 No | SY | Student ID | Student Name | Gender | Section Code | Subject ----------------------------------------------------------------------------- 2015-2016 SE

我正在尝试使用PHP按学年显示一个表。我觉得它似乎不是很好,因为学年显示冗余

如何简单地像这样显示它

No | SY | Student ID | Student Name | Gender | Section Code | Subject ----------------------------------------------------------------------------- 2015-2016 SEMESTER 1 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 1 | 2 | 1212211 | asasas, sasas a. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 2015-2016 SEMESTER 2 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 1 | 2 | aSAsaSA | dsdsadsad.ssasasas | Male | IT15B | IT15 ----------------------------------------------------------------------------- 2016-2017 SEMESTER 1 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15 ----------------------------------------------------------------------------- 2016-2017 SEMESTER 2 ----------------------------------------------------------------------------- 1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15 这是我的密码:

 <tr>
        <th>No</th>
        <th>SY</th>
        <th>Student ID</th>
        <th>Student Name</th>
        <th>Gender</th>
        <th>Section Code</th>
        <th>Subject</th>
        <th colspan="2">Update</th>
    </tr>

$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
              FROM students sts
              JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
              JOIN subjects sub ON (sub.section_code = ets.section_code)
              GROUP BY sts.stud_id, sub.section_code ORDER BY ets.stud_id ASC";

        $sql_sel=mysql_query($sql); 

        $num_rows = mysql_num_rows($sql_sel);                                                               
            if($num_rows==0)
            {
               echo "No results found. Please try again";
            }

    $i=0;
    while($row=mysql_fetch_array($sql_sel)) //for the first query
    {

        $i++;
        $color=($i%2==0)?"lightblue":"white";
    ?>
        <th colspan="8"><?php echo $row['sy'];?></th>  //this thing here

        <tr bgcolor="<?php echo $color?>">
            <td><?php echo $i;?></td>

            <td><?php echo $row['sem'];?></td>
            <td><?php echo $row['stud_id'];?></td>
            <td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
            <td><?php echo $row['gender'];?></td>
            <td><?php echo $row['section_code'];?></td>
            <td><?php echo $row['subject_name'];?></td>  
            <td align="center" width="82"><a href="?tag=enrollment_entry&opr=upd&rs_id=<?php echo $row['enroll_num'];?>" title="Update"><img src="picture/update.png" /></a></td>
            <!----<td align="center"><a href="?tag=view_enrollments&opr=del&rs_id=<?php// echo $row['enroll_num'];?>" title="Delete"><img src="picture/delete.png" /></a></td>-------->
    </tr>
    <?php
    }
    ?>

首先,您需要在查询中按年度和学期排序:

$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
              FROM students sts
              JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
              JOIN subjects sub ON (sub.section_code = ets.section_code)
              GROUP BY sts.stud_id, sub.section_code ORDER BY ets.sy ASC, ets.sem ASC, ets.stud_id ASC";
然后,仅在检测到年份变化时才写入年份行:

    $i=0;
    $yr=null;
        while($row=mysql_fetch_array($sql_sel)) //for the first query
        {

            if ($yr != ($row['sy'] . ' SEMESTER ' . $row['sem'])) {
               $yr = $row['sy'] . ' SEMESTER ' . $row['sem'];
               print ('<th colspan="8">' . $yr . '</th>');
            }

            $i++;
            $color=($i%2==0)?"lightblue":"white";
        ?>
            <tr bgcolor="<?php echo $color?>">
                <td><?php echo $i;?></td>

                <td><?php echo $row['sem'];?></td>
                <td><?php echo $row['stud_id'];?></td>
                <td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
                <td><?php echo $row['gender'];?></td>
                <td><?php echo $row['section_code'];?></td>
                <td><?php echo $row['subject_name'];?></td>  
                <td align="center" width="82"><a href="?tag=enrollment_entry&opr=upd&rs_id=<?php echo $row['enroll_num'];?>" title="Update"><img src="picture/update.png" /></a></td>
                <!----<td align="center"><a href="?tag=view_enrollments&opr=del&rs_id=<?php// echo $row['enroll_num'];?>" title="Delete"><img src="picture/delete.png" /></a></td>-------->
        </tr>
        <?php
        }

您还希望按年份分组,并且不应使用mysql_*函数,因为它们不安全,现在已被弃用。你应该研究一下PDO,你能给我们提供你的数据库结构吗?…或者mysqli。我不明白你的问题。你不应该接受给出的答案吗?剩下的只是修改建议的技术。你的答案非常好,我只想在这学期应用它。也许你可以做一些小的修改,请:。完成了,但正如我所说的,一旦你知道如何处理这一年,修改它以包括这一学期是非常容易的。我已经尝试过了,但在我申请这一学期后,显示中有一个小错误。