gpt4 book ai didi

php - 将 mysql 的结果放在同一行

转载 作者:行者123 更新时间:2023-11-29 19:33:41 25 4
gpt4 key购买 nike

我有一张包含几列的 mysql 表。我想使用 php 在 html 表中显示结果。到目前为止一切顺利,但我想在一行中显示保险名称和金额,但根据付款月份在不同列中显示。

mysql/php 代码:

<html>
<head>
<title>Monatsübersicht</title>
<link href="design.css" type="text/css" rel="stylesheet">
</head>
<?php
include 'connect.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT name_of_insurance, amount, month_of_payment FROM insurance where active = 'on'";
$result = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
echo "<table><tr><th>Name</th><th>Jan</th><th>Feb</th><th>März</th><th>Apr</th><th>Mai</th><th>Jun</th><th>Jul</th><th>Aug</th><th>Sep</th><th>Okt</th><th>Nov</th><th>Dez</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["name_of_insurance"]. "</td><td>" . $row["amount"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($link);

?>

</html>

如何在不存在多个查询或 $result 的情况下将结果放入不同的列?

提前致谢。BR弗洛里安

最佳答案

假设 month_of_ payment 列值采用 Jan、Feb、März 等格式,您的问题的解决方案如下这个:

创建一个月份数组,其中包含 month_of_ payment 列中所有可能的月份值,如下所示:

$monthArr = array('Jan', 'Feb', 'März', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez');

现在,在 while 循环的每次迭代中,创建一个 for 循环来检查 $row["month_of_ payment"] 值属于哪个月份并将其显示在适当的表格单元格中,如下所示:

while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["name_of_insurance"]. "</td>";
for($i = 0; $i < $count; ++$i){
echo "<td>";
if($row["month_of_payment"] == $monthArr[$i]){
echo $row["amount"];
}
echo "</td>";
}
echo "</tr>";
}

其中 $count$monthArr 数组中的元素/月份数,即 $count = count($monthArr);

这是完整代码:

<html>
<head>
<title>Monatsübersicht</title>
<link href="design.css" type="text/css" rel="stylesheet">
</head>
<body>
<?php
include 'connect.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT name_of_insurance, amount, month_of_payment FROM insurance where active = 'on'";
$result = mysqli_query($link, $sql);

$monthArr = array('Jan', 'Feb', 'März', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez');
$count = count($monthArr);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo "<table><tr><th>Name</th><th>Jan</th><th>Feb</th><th>März</th><th>Apr</th><th>Mai</th><th>Jun</th><th>Jul</th><th>Aug</th><th>Sep</th><th>Okt</th><th>Nov</th><th>Dez</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["name_of_insurance"]. "</td>";
for($i = 0; $i < $count; ++$i){
echo "<td>";
if($row["month_of_payment"] == $monthArr[$i]){
echo $row["amount"];
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($link);
?>
</body>
</html>

关于php - 将 mysql 的结果放在同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41650873/

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