gpt4 book ai didi

php - 显示行内的表行

转载 作者:行者123 更新时间:2023-11-29 10:56:35 25 4
gpt4 key购买 nike

我想要实现的目标在下面的示例图中的 html 表中得到了最好的描述。我的表可以很好地处理来自 MySQL 数据库的数据,但所有数据都按行打印,这是正确的,但是我需要以下面的格式和 Excel 中的相同格式显示数据。这可以避免重复类似的数据。

我的SQL语句是这样的:

$query = mysql_query("SELECT * FROM Reservations WHERE Date = '$DateToday' ORDER BY Airline ASC, Flight ASC, Name"); 

但我想要实现所需的显示,php 会比 MySQL 更相关。

Table Ecample所以首先是 while 循环....

while ($row = mysql_fetch_array($query)) { $results = mysql_query("SELECT *      FROM Reservations WHERE Airline = '$Airline'");
$Count = mysql_num_rows($results);
if($Count > 1) // More then 1 passenger on same flight
{
$iReservationID = $row['ReservationID'];
$iAirline = strtoupper($row['Airline']);
$iFlight = strtoupper($row['Flight']);
$iName = strtoupper($row['Name']);
$iName = "<BR>".$iName;
} else {// NOT More then 1 passenger on same flight
$ReservationID = $row['ReservationID'];
$Airline = strtoupper($row['Airline']);
$Flight = strtoupper($row['Flight']);
$Name = strtoupper($row['Name']);
}
}
<table>
<tr>
<td>AIRLINE</td>";
<td>FLIGHT#</td>";
<td>PASSENGER</td>";
</tr>
<tr>
<td>echo $Airline;</td>
<td>echo $Flight;</td>
<td>print "$Name<BR>$iName</td>
</tr>
</table>

当只有一名乘客时,该表可以很好地处理每个条目的行,但在嵌套时会复制乘客姓名。

SELECT DISTINCT SELECT FROM Reservations WHERE ReservationID <> $ReservationID 也不起作用- 我已经尝试了第二次 WHILE循环根据FLIGHT#重新获取数据,但仍然不正确。

非常感谢您的建议。

最佳答案

您可以尝试此代码

<?php
$query = mysql_query("SELECT *,GROUP_CONCAT(Name SEPARATOR '<BR>') AS PasName FROM Reservations WHERE Date = '$DateToday' GROUP BY Airline,Flight ORDER BY Airline ASC, Flight ASC, Name");
echo "<table>
<tr>
<td>AIRLINE</td>
<td>FLIGHT#</td>
<td>PASSENGER</td>
</tr>";
while ($row = mysql_fetch_array($query)) {
//$results = mysql_query("SELECT * FROM Reservations WHERE Airline = '$Airline'");

$ReservationID = $row['ReservationID'];
$Airline = strtoupper($row['Airline']);
$Flight = strtoupper($row['Flight']);
$Name = strtoupper($row['PasName']);

//$Count = mysql_num_rows($results);
/*if ($Count > 1) { // More then 1 passenger on same flight
$iName = "";
while ($inrow = mysql_fetch_array($results)) {
$iName .= "<BR>".$inrow['Name'];
}
} else {// NOT More then 1 passenger on same flight
// $ReservationID = $row['ReservationID'];
// $Airline = strtoupper($row['Airline']);
// $Flight = strtoupper($row['Flight']);
}*/
echo "<tr>
<td>$Airline</td>
<td>$Flight</td>
<td>$Name</td>
</tr>";
}
echo "</table>";
?>

但是,我建议您仅在查询中获取多个用户,即替换此查询

SELECT * FROM Reservations WHERE Airline = '$Airline'

SELECT GROUP_CONCAT(Name SEPARATOR '<BR>') FROM Reservations WHERE Airline = '$Airline' GROUP BY Airline,Flight

这样就不需要内部循环来连接用户名

关于php - 显示行内的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42943342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com