【发布时间】:2023-02-26 17:20:50
【问题描述】:
我需要你的帮助!
我可以按如下方式获取:
| Month | Work |
|---|---|
| 2 | 6 |
| 4 | 1 |
| 5 | 7 |
| 8 | 3 |
| 9 | 5 |
| 11 | 5 |
我希望数据库中的数据看起来像这样;怎么办?
| jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec |
|---|---|---|---|---|---|---|---|---|---|---|---|
| - | 6 | - | 1 | 7 | - | - | 3 | 5 | 6 | 5 | - |
先感谢您。
【问题讨论】:
我需要你的帮助!
我可以按如下方式获取:
| Month | Work |
|---|---|
| 2 | 6 |
| 4 | 1 |
| 5 | 7 |
| 8 | 3 |
| 9 | 5 |
| 11 | 5 |
我希望数据库中的数据看起来像这样;怎么办?
| jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec |
|---|---|---|---|---|---|---|---|---|---|---|---|
| - | 6 | - | 1 | 7 | - | - | 3 | 5 | 6 | 5 | - |
先感谢您。
【问题讨论】:
要从 MySQL 数据库中获取数据并将其显示在特定月份的 HTML 表中,您可以使用 PHP 查询数据库并动态生成 HTML 表。
这是一个示例代码,演示了这一点:
表格的 HTML 代码:
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
查询数据库并生成 HTML 表的 PHP 代码:
<?php
// Define the database connection details
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydatabase";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Define the month and year to fetch data for
$month = "02"; // February
$year = "2023";
// Query the database for data for the specified month and year
$sql = "SELECT name, amount FROM mytable WHERE MONTH(date) = $month AND
YEAR(date) = $year";
$result = mysqli_query($conn, $sql);
// Generate the HTML table dynamically
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["name"] . "</td><td>" . $row["amount"] . "</td>.
</tr>";
}
} else {
echo "<tr><td colspan='2'>No data found</td></tr>";
}
mysqli_close($conn);
?>
此代码在“mydatabase”数据库中的“mytable”表中查询指定月份和年份中具有“date”列的数据,并使用检索到的数据动态生成一个 HTML 表。该表格显示在 HTML 表格的“tableBody”tbody 元素内。
【讨论】: