我正在开发一个时间“打孔”系统,我正在尝试在 PHP 中构建一个表来水平显示 2 个表中的数据:
tb_users 作为你
tb_punch AS p
我希望数据显示如下:
u.user p.punch p.punch p.punch p.punch
+-------+----------+----------+----------+----------+
|User 1 | 10:00:00 | 12:00:00 | 13:00:00 | 19:00:00 |
|User 2 | 08:00:00 | 12:00:00 | | |
|User 3 | 09:00:00 | | | |
+-------+----------+----------+----------+----------+
等等...
但我得到了以下结果:
u.user p.punch p.punch p.punch p.punch p.punch p.punch p.punch
+-------+----------+----------+----------+----------+---------+----------+---------+
|User 1 | 10:00:00 | | | | | | |
|User 1 | 10:00:00 | 12:00:00 | | | | | |
|User 1 | 10:00:00 | 12:00:00 | 13:00:00 | | | | |
|User 1 | 10:00:00 | 12:00:00 | 13:00:00 | 19:00:00 | | | |
|User 2 | 10:00:00 | 12:00:00 | 13:00:00 | 19:00:00 |08:00:00 | | |
|User 2 | 10:00:00 | 12:00:00 | 13:00:00 | 19:00:00 |08:00:00 | 12:00:00 | |
|User 3 | 10:00:00 | 12:00:00 | 13:00:00 | 19:00:00 |08:00:00 | 12:00:00 |09:00:00 |
+-------+----------+----------+----------+----------+---------+----------+---------+
我的意思是,当我想要按用户对打孔进行分组时,它每行重复每个 PUNCH。
这是我尝试执行此操作的方法:
$query = "SELECT u.user, p.punch FROM users u INNER JOIN punches p ON u.id = p.id_user"
$rt = pg_query($query) or die ("Error: [" . $query . "]");
$punches = array();
echo "<tbody>";
while($nt=pg_fetch_assoc($rt)){
echo "<tr>";
echo "<td>" . $nt[user] . "</td>";
$punches[] = $nt[punch];
foreach ($punches as $punch){
echo "<td>" . date('H:i:s',strtotime($punch)) . "</td>";
}
}
echo "</tr></tbody>";
?>
</table>
关于可能出错的任何线索?谢谢!