php 替换空字符串的查询重复项

php replacing query duplicates for empty strings

我有以下脚本来检索 sql 数据:

  // Present data if avail
  if ($result->num_rows > 0) {
   // Query output in tabels
   while($row = $result->fetch_assoc()) {
       echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
       echo '<td class="tg">' .$row["fname"]. '</td>' . "\n";
       echo '<td class="tg">' .$row["lname"]. '</td>' . "\n";
       echo '<td class="tg">' .$row["make"].  '</td></tr>' . "\n";      

   }
   // if no records
  } else {
      echo "0 results";
  }

如何为空字符串替换行 'fname' 中的重复项? (记录按 fname 排序)

您向我们展示的脚本不会检索 SQL 数据 - 它只是显示数据。如果您设法编写了上面的代码,那么这应该是显而易见的:

$prevname='';
while($row = $result->fetch_assoc()) {
   echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
   echo '<td class="tg">' 
      . ( $row["fname"] == $prevname ? '' : $row["fname"])
      . '</td>' . "\n";
   echo '<td class="tg">' .$row["lname"]. '</td>' . "\n";
   echo '<td class="tg">' .$row["make"].  '</td></tr>' . "\n";      
   $prevname=$r["fname"];
}