我一直在尝试在PHP代码中插入一个html表。这样,回波数据(例如,空气读数)可以显示在表格中。请帮忙。
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "100000000000000000";
header("Refresh: $sec; url=$page");
?>
<?php
$server = "localhost";
$user = "root";
$password = "";
$database = "sensors_hub";
// Create connection
$conn = new mysqli($server, $user, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT air_reading, co2_reading, gas_reading, humid_reading, temp_reading FROM sensor_001 ORDER BY time DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> Air Reading: ". $row["air_reading"]. " Carbon Dioxide Reading: ". $row["co2_reading"]." Gas Reading: ". $row["gas_reading"]. " Humidity Reading: ". $row["humid_reading"]. " Temp Reading: ". $row["temp_reading"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
答案 0 :(得分:1)
echo "<table>";
echo "<tr>";
echo "<td>Air Reading:</td>";
echo "<td>Carbon Dioxide Reading:</td>";
echo "<td>Gas Reading:</td>";
echo "<td>Humidity Reading:</td>";
echo "<td>Temp Reading:</td>";
echo "</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td> ". $row["air_reading"]. "</td>";
echo "<td> ". $row["co2_reading"]. "</td>";
echo "<td> ". $row["gas_reading"]. "</td>";
echo "<td> ". $row["humid_reading"]. "</td>";
echo "<td> ". $row["temp_reading"]. "</td>";
}
echo "</tr>";
} else {
echo "0 results";
}
echo "</table>";
这是最大的写作努力,但最容易理解。
如果你想更聪明地做到这一点:
?><table>
<tr>
<td>Air Reading:</td>
<td>Carbon Dioxide Reading:</td>
<td>Gas Reading:</td>
<td>Humidity Reading:</td>
<td>Temp Reading:</td>
</tr>
<?
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?=$row["air_reading"]?></td>
<td><?=$row["co2_reading"]?></td>
<td><?=$row["gas_reading"]?></td>
<td><?=$row["humid_reading"]?></td>
<td><?=$row["temp_reading"]?></td>
</tr>
<?
}
} else {
?>0 results<?
}
?>
</table>
答案 1 :(得分:0)
while()
循环while()
循环while()
循环echo "<table>
<tr>
<th>Air Reading</th>
<th>Carbon Dioxide Reading</th>
<th>Gas Reading</th>
<th>Humidity Reading</th>
<th>Temp Reading</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>". $row["air_reading"]. "</td>
<td>". $row["co2_reading"]."</td>
<td> ". $row["gas_reading"]. "</td>
<td>". $row["humid_reading"]. "</td>
<td>". $row["temp_reading"]. "</td>
</tr>";
}
} else {
echo "<tr><td colspan='5'>0 results</td></tr>";
}
echo "</table>";
答案 2 :(得分:0)
if ($result->num_rows > 0) {
?>
<table>
<tr>
<th>Air Reading<th>
<th>Carbon Dioxide Reading<th>
<th>Gas Reading<th>
<th>Humidity Reading<th>
<th>Temp Reading<th>
</tr>
<?PHP while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php= $row["air_reading"] ?></td>
<td><?php= $row["co2_reading"] ?></td>
<td><?php= $row["gas_reading"] ?></td>
<td><?php= $row["humid_reading"] ?></td>
<td><?php= $row["temp_reading"] ?></td>
</tr>
<?PHP } ?>
} else {
echo "0 results";
}
答案 3 :(得分:0)
你需要在while循环上面创建表,并在完成后结束它。我的回答与hd类似,但我发现这种方式更有用:
{% load fraction_tags %}
{{ my_fraction|pretty_fraction }}
我更喜欢这种方式的原因是它使您的HTML代码更容易阅读,因为您现在可以在HTML代码中回显表,而不是在HTML中间执行整个PHP脚本。