2016-12-18 64 views
0

如果學期的學年與當前表不同,我會創建一個表。Dynamicaly根據列值創建表

從下面的圖片可以看出,「2學期」和「2014學年」的價值基本上是我想要的每個學期和學年都有自己的表格。我不想編碼多個表格,因爲我不確定當前的學年和學期是什麼。 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> 

回答

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> 
+0

文件意外結束。 – nethken

+0

已編輯。缺少1「}」 –

+0

每個數據都有一個表:( – nethken