【发布时间】:2021-04-12 19:53:57
【问题描述】:
我有一个通过SELECT * 进行的记录分组,它提供以下结果:
-----------------------------------------------------------------------
| Room_Location - Room_Name |
-----------------------------------------------------------------------
| Device | Technical | Ergonomic |
-----------------------------------------------------------------------
| Device_Model - Device_Type | ☐ Pass ☐ Fail | ☐ Pass ☐ Fail |
| IP_Address | | |
-----------------------------------------------------------------------
| Device_Model - Device_Type | ☐ Pass ☐ Fail | ☐ Pass ☐ Fail |
| IP_Address | | |
-----------------------------------------------------------------------
then a new table:
-----------------------------------------------------------------------
| Room_Location - Room_Name |
-----------------------------------------------------------------------
| Device | Technical | Ergonomic |
-----------------------------------------------------------------------
| Device_Model - Device_Type | ☐ Pass ☐ Fail | ☐ Pass ☐ Fail |
| IP_Address | | |
-----------------------------------------------------------------------
| Device_Model - Device_Type | ☐ Pass ☐ Fail | ☐ Pass ☐ Fail |
| IP_Address | | |
-----------------------------------------------------------------------
等等……
我知道有些人反对将代码与 HTML 交织在一起,但撇开这一点不谈,我遇到的是: 第一条记录的房间代码和房间名称没有出现在第一条记录的上方,而是作为第二条记录的标题出现,并向下延伸到最后一条记录。
最后一条记录实际上是上面房间记录的房间代码,在它的标题中。
它正确地对项目进行分组,只是将标题详细信息放在错误的区域,几乎就像它需要 -1 记录集但仅用于房间位置和房间名称。
我的连接代码:
$result = mysqli_query($con,"SELECT * FROM RoomList ORDER BY Room_Code ASC");
余数:
<?php
/*// Start record fail error checking
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
// Start record fail error checking */
if (mysqli_num_rows($result) === 0) {?>
<div id="resultserror">
No records could found for the selected view.</div>
<?php } else { ?>
<?php ;
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$itemName = $row["Room_Code"];
if (!array_key_exists($itemName, $data)) {
$data[$itemName] = array();
}
$data[$itemName][] = $row;
}
foreach ($data as $itemName => $rows) {
?>
<table id="results" width="100%" cellpadding="3" cellspacing="0">
<tr><td colspan="4"><?php echo($row['Room_Code']);?> - <?php echo($row['Room_Name']);?></td></tr>
<tr>
<th width="" align="center">Device</th>
<th width="100" align="center">Technical</th>
<th width="100" align="center">Ergonomic</th>
</tr>
<?php
foreach ($rows as $row) {
?>
<tr>
<td align='left'>
<strong>Device:</strong><?php echo($row['Device_Model']);?><br>
<strong>Type:</strong> <?php echo($row['Device_Type']);?><br>
<strong>IP Address:</strong> <?php echo($row['IP_Address']);?></td>
<td align='center'>☐ Pass<br>
☐ Fail</td>
<td align='center'>☐ Pass<br>
☐ Fail</td>
</tr>
<?php
}
?>
<tr>
<td colspan="7" align="center">- end of room report -</td></tr>
</table>
<?php
}
mysqli_close($con);
}?>
<div id="reportgen"><?php echo("Report Generated: " . date("Y-m-d H:i:s")); ?></div>
<div id="reportcount"><?php echo("Record Count: " . mysqli_num_rows($result) . "");?></div>
【问题讨论】:
标签: php mysql while-loop