2016-12-18 15 views
0

の値がの場合、別のテーブルを作成します。は現在のテーブルとは異なります。列の値に基づいてテーブルを動的に作成する

下の画像から、「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

関連する問題