2016-12-18 37 views
1

我想第一學期S.Y 2013只顯示一次。但在我的代碼中,它顯示多次,因爲它在循環內部。有沒有其他方法可以做到這一點?如何在循環內只顯示一次提取的數據

這裏是我的代碼的輸出。 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> 
    </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></td> 
    <td><?php echo $row['semester']. "st Semester S.Y " .$row['sch_year'];?></td> 
    <td></td> 
    <td></td> 
    </tr> 
    <tr> 
     <td><?php echo $row['subj_cd'];?></td> 
     <td><?php echo $row['subj_descr'];?></td> 
     <td><?php echo $row['final_grade'];?></td> 
     <td><?php echo $row['units_lec'] + $row['units_lab'];?></td> 
    </tr> 

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

回答

1
<?php 
    $sql ="SELECT * FROM grades WHERE stud_no ='$stud_no'"; 
    $result = mysqli_query($con, $sql); 
    var $check=1; //added line 
    while($row = mysqli_fetch_array($result)){     
    ?> 

然後用,如果條件實現自己的目標

<td><?php 
if($check==1){ 
echo $row['semester']. "st Semester S.Y " .$row['sch_year']; 
$check=0; 
} 
?></td> 
相關問題