动态创建基于列值的表

时间:2016-12-18 09:53:29

标签: php html sql

如果学期学年的值与当前表不同,我想创建另一个表。

从下面的照片中可以得到" 2学期"和#34; 2014学年和#34;基本上我想要的是每个学期和学年都有自己的表。我不想编写多个表格,因为我不确定当前学年和学期是什么。 enter image description here

这是我的代码

<div class="table-responsive">
<table class="table table-bordered">

  <thead>

    <tr>
      <th>Subject Code</th>
      <th>Decription</th>
      <th>Grade</th>
      <th>Units</th>
       <th>Semester</th>
        <th>School Year</th>
    </tr>
  </thead>
  <tbody>
   <?php
   $sql ="SELECT * FROM grades WHERE stud_no ='$stud_no'";
   $result = mysqli_query($con, $sql);
  while($row = mysqli_fetch_array($result)){                   
  ?>

    <tr>
      <td><?php echo $row['subj_cd'];?></td>
      <td><?php echo ucwords(strtolower($row['subj_descr']));?></td>
      <td><?php echo $row['final_grade'];?></td>
      <td><?php echo $row['units_lec'] + $row['units_lab'];?></td>
      <td><?php echo $row['semester'];?></td>
      <td><?php echo $row['sch_year'];?></td>
    </tr>

  </tbody>
<?php
}
?>
</table>
</div>

1 个答案:

答案 0 :(得分:0)

尝试以下代码。希望它有所帮助。 :)

    <?php
    //Order year asc then semester asc

                   $sql ="SELECT * FROM grades WHERE stud_no ='$stud_no' ORDER BY sch_year, semester";
                   $result = mysqli_query($con, $sql);

                    // Init 2 empty variable for sem and year
                    $prev_year = $prev_sem = '';

                  while($row = mysqli_fetch_array($result)){                   
                  ?>

                    <tr>
                      <td><?php echo $row['subj_cd'];?></td>
                      <td><?php echo ucwords(strtolower($row['subj_descr']));?></td>
                      <td><?php echo $row['final_grade'];?></td>
                      <td><?php echo $row['units_lec'] + $row['units_lab'];?></td>
                      <td><?php echo $row['semester'];?></td>
                      <td><?php echo $row['sch_year'];?></td>
                    </tr>
                <?php

        // Check is $prev_sem is empty or not. If empty, assign it. As $prev_sem and $prev_year will be set at the same time, it can skip to check $prev_year
                if (!empty($prev_sem)) {
                    $prev_sem = $row['semester'];
                    $prev_year = $row['sch_year'];
                }

        // Check if previous sem and year is not same, print the HTML code below.
                if ($prev_sem != $row['semester'] || $prev_year != $row['sch_year']) {

                    print '
                </tbody>
                </table>
                </div>
                <div class="table-responsive">
                <table class="table table-bordered">

                  <thead>

                    <tr>
                      <th>Subject Code</th>
                      <th>Decription</th>
                      <th>Grade</th>
                      <th>Units</th>
                       <th>Semester</th>
                        <th>School Year</th>
                    </tr>
                  </thead>
                  <tbody>

                   ';
                }
}
            ?>
              </tbody>