Php - PostgreSQL 将数据回显到一个单元格中 (HTML Table)
Php - PostgreSQL echo data into one cell (HTML Table)
我正在尝试以如下所示的格式从数据库中回显数据;
|column-1|column-2|column-3|column-4|column-5|
|--------|--------|--------|--------|--------|
|My Name | Date |Message | People |Phone #s|
| People |Phone #s|
| People |Phone #s|
| People |Phone #s|
| People |Phone #s|
这意味着,我从数据库中选择的数据是这样的:第 4、5 列中的人从第 1 列中的人那里收到了消息(第 3 列)。但是根据我从数据库回显数据的代码,我在第 4 列和第 5 列中为每个其他人创建了一个新列,如下所示;
|column-1|column-2|column-3|column-4|column-5|
|--------|--------|--------|--------|--------|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
下面是我的 php 生成 table;
的代码
echo "<table id='table'>";
while($row=pg_fetch_assoc($result)){echo "<tr>";
echo "<td align='left' width='200'>" . $row['message_by'] . "</td>";
echo "<td align='left' width='200'>" . $row['message_date'] . "</td>";
echo "<td align='left' width='200'>" . $row['message_text'] . "</td>";
echo "<td align='left' width='200'>" . $row['phone_number'] . "</td>";
echo "<td align='left' width='200'>" . $row['recipient_name'] . "</td>";
echo "</tr>";}
echo "</table>";
所以问题是如何将第 4 列和第 5 列的数据输出到单个单元格中,或者将数据回显到不同的单元格中而不重复第 1 列到第 3 列?
处理您的问题的典型方法是构建一个小型状态机来跟踪列值的变化。在您的情况下,如果前三列的任何值发生变化,那么您想要打印一条完整记录。否则,只打印最后两列。下面使用的技巧是我们总是放下 5 <td>
标签,但对于重复行,我们只将空字符串分配到这些单元格中。
$col1 = NULL;
$col2 = NULL;
$col3 = NULL;
echo "<table id='table'>";
while ($row = pg_fetch_assoc($result)) {
$text1 = '';
$text2 = '';
$text3 = '';
if ($row['message_by'] != $col1 ||
$row['message_date'] != $col2 ||
$row['message_text'] != $col3) {
$col1 = $row['message_by'];
$col2 = $row['message_date'];
$col3 = $row['message_text'];
$text1 = $col1;
$text2 = $col2;
$text3 = $col3;
}
echo "<tr>";
echo "<td align='left' width='200'>" . $text1 . "</td>";
echo "<td align='left' width='200'>" . $text2 . "</td>";
echo "<td align='left' width='200'>" . $text3 . "</td>";
echo "<td align='left' width='200'>" . $row['phone_number'] . "</td>";
echo "<td align='left' width='200'>" . $row['recipient_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
重要提示: 只有当您的 Postgres 查询使用 ORDER BY
对结果集进行排序时,此答案才有效,并且才有意义。特别是,查询应该按照首先排序的三个消息列进行排序。
我正在尝试以如下所示的格式从数据库中回显数据;
|column-1|column-2|column-3|column-4|column-5|
|--------|--------|--------|--------|--------|
|My Name | Date |Message | People |Phone #s|
| People |Phone #s|
| People |Phone #s|
| People |Phone #s|
| People |Phone #s|
这意味着,我从数据库中选择的数据是这样的:第 4、5 列中的人从第 1 列中的人那里收到了消息(第 3 列)。但是根据我从数据库回显数据的代码,我在第 4 列和第 5 列中为每个其他人创建了一个新列,如下所示;
|column-1|column-2|column-3|column-4|column-5|
|--------|--------|--------|--------|--------|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
|My Name | Date |Message | People |Phone #s|
下面是我的 php 生成 table;
的代码echo "<table id='table'>";
while($row=pg_fetch_assoc($result)){echo "<tr>";
echo "<td align='left' width='200'>" . $row['message_by'] . "</td>";
echo "<td align='left' width='200'>" . $row['message_date'] . "</td>";
echo "<td align='left' width='200'>" . $row['message_text'] . "</td>";
echo "<td align='left' width='200'>" . $row['phone_number'] . "</td>";
echo "<td align='left' width='200'>" . $row['recipient_name'] . "</td>";
echo "</tr>";}
echo "</table>";
所以问题是如何将第 4 列和第 5 列的数据输出到单个单元格中,或者将数据回显到不同的单元格中而不重复第 1 列到第 3 列?
处理您的问题的典型方法是构建一个小型状态机来跟踪列值的变化。在您的情况下,如果前三列的任何值发生变化,那么您想要打印一条完整记录。否则,只打印最后两列。下面使用的技巧是我们总是放下 5 <td>
标签,但对于重复行,我们只将空字符串分配到这些单元格中。
$col1 = NULL;
$col2 = NULL;
$col3 = NULL;
echo "<table id='table'>";
while ($row = pg_fetch_assoc($result)) {
$text1 = '';
$text2 = '';
$text3 = '';
if ($row['message_by'] != $col1 ||
$row['message_date'] != $col2 ||
$row['message_text'] != $col3) {
$col1 = $row['message_by'];
$col2 = $row['message_date'];
$col3 = $row['message_text'];
$text1 = $col1;
$text2 = $col2;
$text3 = $col3;
}
echo "<tr>";
echo "<td align='left' width='200'>" . $text1 . "</td>";
echo "<td align='left' width='200'>" . $text2 . "</td>";
echo "<td align='left' width='200'>" . $text3 . "</td>";
echo "<td align='left' width='200'>" . $row['phone_number'] . "</td>";
echo "<td align='left' width='200'>" . $row['recipient_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
重要提示: 只有当您的 Postgres 查询使用 ORDER BY
对结果集进行排序时,此答案才有效,并且才有意义。特别是,查询应该按照首先排序的三个消息列进行排序。