我的代码只是查询数据库并输出到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,然后移动指针?),请帮忙,谢谢!!
答案 0 :(得分: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'];
}
P.S。我永远不会尝试在SQL中执行此操作,因为它实际上是特定于此特定上下文中的数据表示方式,而不是数据本身的性质。在PHP中也更简单。
答案 1 :(得分:0)
在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>";