0

我正在开发一个时间“打孔”系统,我正在尝试在 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>

关于可能出错的任何线索?谢谢!

4

4 回答 4

1

我不建议您将 SQL 查询与表示层或工作流层混合使用。使用 Pomm 将允许您查询数据库,如下所示:

SELECT 
 u.user,
 ... -- other users fields
 array_agg(p.punch) AS punch_times 
FROM 
 users u 
   INNER JOIN punchs p ON u.id = p.id_user
GROUP BY
  u.user,
  ... -- other users fields

并在名为punch_times. 所以你的模板会变成:

<tbody>
<?php foreach ($users as $user): ?>
  <tr>
    <td><?php echo $user['user'] ?></td>
    <td>
      <ul>
      <?php foreach ($user['punch-times'] as $punch_time): ?>
        <li><?php echo $punch_time->format('H:i:s') ?></li>
      <?php endforeach ?>
    </td>
  </tr>
<?php endforeach ?>
</tbody>
于 2012-09-17T07:57:29.763 回答
0

尝试添加GROUP BY u.id到您的查询中。

于 2012-09-13T19:34:37.227 回答
0

试试SELECT u.user, string_agg(p.punch,',') AS punches FROM users AS u JOIN punches AS p ON u.id=p.id_user GROUP BY u.user

然后像

while($nt = pg_fetch_assoc($rt)) {
    echo "<tr><td>{$nt['user']}</td>";
    echo "<td>",implode('</td><td>',explode(',',$nt['punches'])),"</td>";
    echo "</tr>";
}

PS 另外,您的代码似乎产生了不平衡的<tr>/ </tr>

于 2012-09-13T19:34:40.537 回答
0

好吧,在我一个朋友的帮助下,我想通了。

我没有更改查询语句,只是标记了一些在特定条件下显示的 TR 和 TD。这是我所做的:

echo "<tbody>";
$prev_user = "";
$flag = true;

while($nt=pg_fetch_assoc($rt)){

    if ($flag) {
        echo "<tr>";
        echo "<td>" . $nt[user] . "</td>";
        $prev_user = $nt[user];
    }

    if ($prev_user != $nt[user]) {
        echo "</tr>";
        echo "<tr>";
        echo "<td>" . $nt[user] . "</td>";
    }

    echo "<td>" . date('H:i:s',strtotime($nt[punch])) . "</td>";

    $prev_user = $nt[user];

    $flag = false;

}

echo "</tbody>";

这解决了我的问题,现在我得到了这样的表格:

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 |          |          |          |
+-------+----------+----------+----------+----------+
于 2012-09-14T13:23:35.820 回答