我有一个包含多个列的mysql表。 使用php我想在html表中显示结果。 到目前为止一切都那么好,但是我想在一行中显示name_of_insurance和金额,但是根据month_of_payment不同的列数。
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 弗洛里安
答案 0 :(得分:0)
假设 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>
答案 1 :(得分:0)
感谢Rajdeep Paul: 我的问题的正确答案如下:
<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', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
$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>