我想在html表格中的下面的示例图中最好地描述。我的表适用于来自MySQL数据库的数据,但所有数据都是按行打印的,这是正确的,但是我需要以下面的格式显示数据,并在Excel中显示相同的格式。这可以避免重复类似的数据。
我的SQL语句是:
$query = mysql_query("SELECT * FROM Reservations WHERE Date = '$DateToday' ORDER BY Airline ASC, Flight ASC, Name");
但我想要实现所需的显示php会比MySQL更有意义。
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#重新获取数据但仍然不正确。
建议非常感谢。
答案 0 :(得分:1)
您可以尝试此代码
<?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
这样就不需要内部循环来连接用户名
答案 1 :(得分:0)
试试这个
$sql = "SELECT * FROM Reservations WHERE Date = '$DateToday' ORDER BY Airline ASC, Flight ASC, Name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table>
<tr>
<td>AIRLINE</td>
<td>FLIGHT#</td>
<td>PASSENGER</td>
</tr>
";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>"+$row['ReservationID']+"</td>
<td>"+strtoupper($row['Airline'])+"</td>
<td>"+strtoupper($row['Flight'])+"</td>
<td>"+MethodToFetchNameOfPassengersOfTheFlight(params)+"</td>
</tr>";
echo "</table>";
}
方法
string MethodToFetchNameOfPassengersOfTheFlight(params){
//write a query to fetch the name of the passenger of the flight
$CustomerNames = "";
//in for each loop do this
foreach($name in $names)
$CustomerNames += $name + "<br>";
return strtoupper($CustomerNames);
}
首先,我们展示航空公司的详细信息。然后我们找到相应航班的乘客并将它们串在一个断裂的字符串中,然后将它们分开。