【问题标题】:PHP - skip display duplicate fields(not records) in MySQL queryPHP - 在 MySQL 查询中跳过显示重复字段(不是记录)
【发布时间】:2019-01-13 09:06:28
【问题描述】:

我的代码只是查询数据库并输出到 HTML 表:

<?php

  include("incl\dbcon.php");

  $sql = "SELECT * FROM attendanceRecord";

  $result = $db_con->query($sql);

  echo "<table>
  <tr>
  <th>Date</th>
  <th>Building Name</th>
  <th>Room Name</th>
  <th>Student</th>
  <th>Time</th>
  </tr>";

  while($row_result = $result->fetch_assoc()){
    echo "<tr>";
    echo "<td>" . $row_result['rDate'] . "</td>";
    echo "<td>" . $row_result['rBuildingName'] . "</td>";
    echo "<td>" . $row_result['rRoomName'] . "</td>";
    echo "<td>" . $row_result['rStudent'] . "</td>";
    echo "<td>" . $row_result['rTime'] . "</td>";
    echo "</tr>";
  }

  $db_con->close();

  echo "</table>";

?>

结果是这样的:

Date         Building Name    Room Name  Student    Time
2018-07-12   Building A       1A         Sam        08:32:33
2018-07-12   Building A       1A         David      08:54:21
2018-07-12   Building A       1A         Dragon     08:50:10
2018-07-12   Building A       1B         John       08:43:11
2018-07-12   Building A       1B         Coco       08:51:39
2018-07-12   Building B       3A         Mary       08:21:23
2018-07-12   Building B       3A         Martin     08:46:57
2018-07-12   Building B       4B         Ray        08:26:47

如何不显示或清空连续的重复字段并使表格如下所示?

Date         Building Name    Room Name  Student    Time
2018-07-12   Building A       1A         Sam        08:32:33
                                         David      08:54:21
                                         Dragon     08:50:10
                              1B         John       08:43:11
                                         Coco       08:51:39
             Building B       3A         Mary       08:21:23
                                         Martin     08:46:57
                              4B         Ray        08:26:47

我应该在哪里接近?来自sql查询(不知道查询时是否有清空或NULL重复字段的功能)或php数组(如果重复则打印NULL然后移动指针?),请帮助,提前谢谢!

【问题讨论】:

  • 你为什么要在 mysql 端这样做?这是数据呈现的问题,应该在应用的视图逻辑中完成。
  • 感谢您的评论,我不坚持在mysql方面这样做,只是试图以适当的方式找到解决方案。

标签: php mysql


【解决方案1】:

假设行总是以正确的顺序出现(您可以使用 ORDER BY 来确保它!)然后在 PHP 中,保留一个包含上一行日期值的变量。如果它与当前行上的日期匹配,则不要在 td 中打印该值。如果没有,则打印它,因为它必须是新的。建筑物名称和房间名称的方法相同,但在当前日期(和建筑物)的上下文中:

$date = "";
$buildingName = ""; //declare these outside the loop so they persist between loops
$roomName = "";

while($row_result = $result->fetch_assoc()){
    echo "<tr>";
    echo "<td>" . ($row_result['rDate'] == $date ? "" : $row_result['rDate']) . "</td>";
    echo "<td>" .($row_result['rBuildingName'] == $buildingName $row_result['rDate'] == $date ? "" : $row_result['rBuildingName']) . "</td>";
    echo "<td>" . ($row_result['rRoomName'] == $roomName && $row_result['rBuildingName'] == $buildingName && $row_result['rDate'] == $date ? "" : $row_result['rRoomName']) . "</td>";
    echo "<td>" . $row_result['rStudent'] . "</td>";
    echo "<td>" . $row_result['rTime'] . "</td>";
    echo "</tr>";
    //update the references to the specific fields
    $date = $row_result['rDate'];
    $buildingName = $row_result['rRoomName'];
    $roomName = $row_result['rRoomName'];
}

附:我永远不会尝试在 SQL 中执行此操作,因为它确实特定于此特定上下文中的数据表示,而不是数据本身的性质。它在 PHP 中也简单得多。

【讨论】:

  • 感谢您的回答!今晚会试一试,然后回复你!
  • 哥们,你的答案正是我想要的,谢谢指教!
【解决方案2】:

在 PHP 中你可以做这样的事情

  include("incl\dbcon.php");

  $sql = "SELECT * FROM attendanceRecord";

  $result = $db_con->query($sql);

  echo "<table>
  <tr>
  <th>Date</th>
  <th>Building Name</th>
  <th>Room Name</th>
  <th>Student</th>
  <th>Time</th>
  </tr>";

  $rDate = $rBuildingName = $rRoomName = $rStudent = $rTime = [];

  while($row_result = $result->fetch_assoc()){

    echo "<tr>";
    echo "<td>" . (!in_array(  $row_result['rDate'] , $rDate) ? $row_result['rDate'] : ''). "</td>";
    echo "<td>" . (!in_array(  $row_result['rBuildingName'] , $rBuildingName) ? $row_result['rBuildingName'] : '') . "</td>";
    echo "<td>" . (!in_array(  $row_result['rRoomName'] , $rRoomName) ? $row_result['rRoomName'] : '') . "</td>";
    echo "<td>" . (!in_array(  $row_result['rStudent'] , $rStudent) ? $row_result['rStudent'] : '') . "</td>";
    echo "<td>" . (!in_array(  $row_result['rTime'] , $rTime) ? $row_result['rTime'] : '') . "</td>";
    echo "</tr>";

    $rDate[] = $row_result['rDate'];
    $rBuildingName[] = $row_result['rBuildingName'];
    $rRoomName[] = $row_result['rRoomName'];
    $rStudent[] = $row_result['rStudent'];
    $rTime[] = $row_result['rTime'];


  }

  $db_con->close();

  echo "</table>";

【讨论】:

  • OP为什么要做这样的事情好的答案将始终解释所做的事情以及为什么以这种方式完成,不仅适用于 OP,而且适用于 SO 的未来访问者。
  • @B001ᛦ 我认为它是不言自明的,应该在这里解释三元运算符吗?
  • 感谢您的回答!今晚会消化并尝试!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多