如何使用此格式在HTML表中打印此数据?
想象一下,您有此SQL查询结果:
ID ID_KEY VALUE
1 1 Text1.1
2 1 Text1.2
3 1 Text1.3
4 2 Text2.1
5 2 Text2.2
6 2 Text2.3
7 3 Text3.1
8 3 Text3.2
9 3 Text3.3
您想考虑 id_key ,像这样:
ID_KEY VALUE1 VALUE2 VALUE3
1 Text1.1 Text1.2 Text1.3
2 Text2.1 Text2.2 Text2.3
3 Text3.1 Text3.2 Text2.3
我该怎么做?当ID_Key更改时,我想打印新的行。 例如,现在我有了此代码:
$result = $con->query($sql);
if ($result->num_rows > 0) {
$res = "<table>";
$res .= "<tr>";
$res .= "<th>ID_KEY</th>";
$res .= "<th>VALUE1</th>";
$res .= "<th>VALUE2</th>";
$res .= "<th>VALUE3</th>";
$res .= "</tr>";
while ($row=mysqli_fetch_assoc($result)) {
$res .= "<tr>";
$res .= "<td>" . $row['ID_KEY'] . "</td>";
$res .= "<td>" . $row['VALUE1'] . "</td>";
$res .= "<td>" . $row['VALUE2'] . "</td>";
$res .= "<td>" . $row['VALUE3'] . "</td>";
$res .= "</tr>";
}
$res .= "</table>";
return $res;
}
此代码不起作用,因为“ value1”,“ value2”和“ value3”是我表中不存在的字段。
相反,我这么说:
$res .= "<tr>";
$res .= "<td>" . $row['ID_KEY'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "</tr>";
这也不起作用,因为“值”的值将重复3次。
是否可以这样做,还是我应该重组数据库以另一种方式存储信息?
Imagine that you have this SQL query result:
ID ID_KEY VALUE
1 1 Text1.1
2 1 Text1.2
3 1 Text1.3
4 2 Text2.1
5 2 Text2.2
6 2 Text2.3
7 3 Text3.1
8 3 Text3.2
9 3 Text3.3
And you want to print a table taking into account the ID_KEY, like this:
ID_KEY VALUE1 VALUE2 VALUE3
1 Text1.1 Text1.2 Text1.3
2 Text2.1 Text2.2 Text2.3
3 Text3.1 Text3.2 Text2.3
How can I do that? I want to print a new row when ID_KEY changes.
For example, now I have this code:
$result = $con->query($sql);
if ($result->num_rows > 0) {
$res = "<table>";
$res .= "<tr>";
$res .= "<th>ID_KEY</th>";
$res .= "<th>VALUE1</th>";
$res .= "<th>VALUE2</th>";
$res .= "<th>VALUE3</th>";
$res .= "</tr>";
while ($row=mysqli_fetch_assoc($result)) {
$res .= "<tr>";
$res .= "<td>" . $row['ID_KEY'] . "</td>";
$res .= "<td>" . $row['VALUE1'] . "</td>";
$res .= "<td>" . $row['VALUE2'] . "</td>";
$res .= "<td>" . $row['VALUE3'] . "</td>";
$res .= "</tr>";
}
$res .= "</table>";
return $res;
}
This code won't work because "value1", "value2" and "value3", are fields that do not exist in my table.
If instead I say this:
$res .= "<tr>";
$res .= "<td>" . $row['ID_KEY'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "</tr>";
This won't work either, as the value of "VALUE" will be repeated 3 times.
Is it possible to do this or should I restructure the database to store the information in another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的代码可以解决问题,但取决于数据库和查询的内容。确保总有3个值每个
ID_KEY
,并且结果进行正确排序。这个想法是,您重复具有值的输出单元格,直到id_key
更改。可能会有许多较小的变化,但最终它们都按照这一原则进行。The code below would do the trick, but it is dependent on the content of your database and the query. Make sure there are always 3 values per
ID_KEY
and that the results are sorted properly. The idea is that you repeat to output cells with a value, until theID_KEY
changes. Many minor variations are possible, but in the end they all work on this principle.不是最优雅的,但是您可以先进行每组,然后通过分组数据进行循环以进行输出。但是,使用SQL以这种方式将数据分组可能会更有效。
Not the most elegant but you could group each set first and and loop through the grouped data for the output. However it would probably more efficient using SQL to group the data this way.