如何在循环内

时间:2016-12-18 04:09:22

标签: php html sql

我希望第一学期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 个答案:

答案 0 :(得分: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)){                   
  ?>

然后使用if条件来实现您的目标

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