gpt4 book ai didi

php - 如何使html表列根据mysql中选定的列动态更改

转载 作者:行者123 更新时间:2023-11-29 15:42:08 26 4
gpt4 key购买 nike

我有多个选择选项。但是,我希望当我选择 1 个或两个选项或其他选项时更改列 <th></th>动态生成html并从数据库获取数据

目前我已经创建了多重选择选项和 html 表格

<select class="selectpicker" id="sensor" name="sensor[]" multiple>
<option
value="temperature">Temperature</option>
<option value="humidity">Humidity</option>
<option value="presure">Presure</option>
<option
value="level">Level</option>
</select>

$query = "SELECT ".$selectedSensorOption." from p1";
$result = $db_handle->runQuery($query);
foreach ($result as $key => $value) {
<table>
<tr>
<th>$result</th>
</tr>
<tr>
<td>$result</td>

我还想根据多个选择选项从 MySQL 数据库获取动态列和字段

最佳答案

一种直接的方法是使用结果的键值作为标题,然后将每行数据作为值输出(代码中的注释)...

// Output table start and header row
echo "<table><tr>";
// Use the first rows results as the header values
foreach ( $result[0] as $header => $value ) {
echo "<th>{$header}</th>";
}
echo "</tr>";
// Output each data row
foreach ($result as $values) {
echo "<tr>";
// Loop over each item in the row and output the value
foreach ( $values as $value ) {
echo "<td>{$value}</td>";
}
echo "</tr>";
}
echo "</table>";

您可以创建更短的代码(使用implode()等),但有时更简单的代码更容易开始和维护。

使用返回的列名称意味着您可以使用列别名来使用特定标题 - 类似于

id as `User ID`, fullname as `Name`

关于php - 如何使html表列<th></th>根据mysql中选定的列动态更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57506034/

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