如何在使用mysqli php更改特定值时在输出中放置一个中断


How to put a break in the output when specific values change using mysqli php

我正在建立一个网站,列出过去24年保龄球锦标赛的统计数据。使用以下代码生成一个显示所有数据的长单表。当$row['season']值发生变化时,即从1990-1991年到1991-1992年,并且对于季节的每个后续变化,并在季节之间回声html水平线或将来自数据库的季节值(即2013-2014)放在每个表段的顶部时,我想在表中添加一个断点。在网上搜了一个星期也没找到答案。这是我现在的代码。必须是mysqli

$result = mysqli_query($conn,"SELECT * FROM members INNER JOIN scores ON members.id=scores.memberID WHERE format LIKE '%s%' ORDER BY year, STR_TO_DATE( month, '%b' ), format ASC;");
echo "<table border='0'>
<tr>
<th>Name</th>
<th>Hometown</th>
<th>Month</th>
<th>Year</th>
<th>Season</th>
<th>Center</th>
<th>Center City</th>
<th>Format</th>
</tr>";
foreach($result as $row) {
  echo "<tr>";
  echo "<td>" . $row['firstName'] . " ". $row['lastName'] . "</td>";
  echo "<td>" . $row['hometown'] . "</td>";
  echo "<td>" . $row['month'] . "</td>";
  echo "<td>" . $row['year'] . "</td>";
  echo "<td>" . $row['season'] . "</td>";
  echo "<td>" . $row['center'] . "</td>";
  echo "<td>" . $row['centerCity'] . "</td>";
  echo "<td>" . $row['format'] . "</td>";
  echo "</tr>";
}
echo "</table>";
mysqli_close($conn);

答案实际上已经给出了,但它看起来像你新的php,所以这里是一个小的例子,我希望它对你有用。您需要插入一些if语句来检查年份是否有变化。对于您想要执行的任何其他检查也是如此。

这是你可以做的…

<?php
    $lastYear = null;//you can also set the first year manually of course
    foreach($result as $row) {
         //set the first year
         if($lastYear == null){$lastYear = $row['year'];}
         //check if the year changed or not
         if($lastYear == $row['year']){
             //if the year didnt change... do something
         }else{
             //your year changed... do something different
             $lastYear = $row['year']; //update your 'last'year
         }
    }
?>