我有多个选择选项。但是,我想要当我选择1个选项或两个或其他选项以动态更改html的列<th></th>
并从数据库获取数据
当前,我已经创建了多选选项和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数据库中获取动态列和字段
答案 0 :(得分:1)
一种简单的方法是使用结果的键值作为标题,然后将每一行数据作为值输出(代码中的注释)...
// 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`
答案 1 :(得分:0)
我用过AJAX。希望对您有所帮助; D
read.php
<?php
//include header
header('Content-Type: application/json');
$conn= mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$type = $_GET["type"];
if($type == "GetCategory"){
//SAMPLE QUERY
$sql = "SELECT CategoryID,CategoryName from Category";
$data = array();
$results = $db -> query($sql);
while($row = mysqli_fetch_assoc($results)){
$data[] = $row;
}
echo json_encode($data);
}
if($type == "GetSubCategory"){
$CategoryID = $_POST["CategoryID"];
//SAMPLE QUERY
//LET'S ASSUME THAT YOU HAVE FOREIGN KEY
$sql = "SELECT SubCategoryID,SubCategoryName from SubCategory where CategoryID = '".$CategoryID."'";
$data = array();
$results = $db -> query($sql);
while($row = mysqli_fetch_assoc($results)){
$data[] = $row;
}
echo json_encode($data);
}
?>
index.html
<select id="category"></select>
<select id="subcategory"></select>
<!--PLEASE INCLUDE JQUERY RIGHT HERE E.G. <script src='jquery.min.js'></script>-->
<!--DOWNLOAD JQUERY HERE https://jquery.com/-->
<script>
LoadCategory();
function LoadCategory(){
$.ajax({
url:"read.php?type=GetCategory",
type:"GET",
success:function(data){
var options = "<option selected disabled value="">Select
Category</option>";
for(var i in data){
options += "<option value='"+data[i].CategoryID+"'>" + data[i].CategoryName + "</option>";
}
$("#category").html(options);
}
});
}
function LoadSubCategory(CategoryID){
$.ajax({
url:"read.php?type=GetSubCategory",
type:"POST",
data:{
CategoryID : CategoryID
},
success:function(data){
var options = "<option selected disabled value="">Select Sub-Category</option>";
for(var i in data){
options += "<option value='"+data[i].SubCategoryID+"'>" + data[i].SubCategoryName + "</option>";
}
$("#subcategory").html(options);
}
});
}
$("#category").change(function(){
LoadSubCategory(this.value);
});
答案 2 :(得分:-1)
首先,您必须获取标题,可以使用explode获取标题键。 您将使用两个循环,第一个循环用于构建标头,第二个循环用于构建主体。在主体循环中,您必须使用另一个循环才能为每一列获取正确的值
<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>
<?php
$query = "SELECT ".$selectedSensorOption." from p1";
$results = $db_handle->runQuery($query);
//get the headers if $selectedSensorOption is not an array
$headers = explode(',', $selectedSensorOption)
?>
<table>
<thead>
<tr>
<!-- loop and fill the header -->
<?php foreach ($headers as $header) : ?>
<th><?= ucfirst($header) ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<!-- loop and fill the body-->
<?php foreach ($results as $result) : ?>
<tr>
<!-- loop and fill each column -->
<?php foreach ($headers as $header) : ?>
<th><?= $result[$header] ?></th>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>