gpt4 book ai didi

php - 从不同的表中获取并添加下拉菜单

转载 作者:行者123 更新时间:2023-11-29 16:37:43 26 4
gpt4 key购买 nike

我已经有了一个启动连接,我想添加另一个下拉菜单,该菜单将从不同的表中获取数据,并在我尝试工作时将其分配给所有用户,但它只显示一个用户,而数据库中有很多用户。当我删除下拉菜单时,它会显示数据库中的所有用户。

 <div class="table-responsive">
<?php
include 'config.php';
$sql = "SELECT * FROM tbl_department";
$result = $conn->query($sql);


if ($result->num_rows > 0) {?>

<table>
<tr>
<th>NO</th>
<th>Department</th>
<th>Status</th>
<th>Action</th>
</tr>

<tbody>
<?php
$no = 1;
while($row = $result->fetch_assoc()) {
$session = $row['session'];
if ($session == "AM") {
$st = 'Morning';
}else{
$st = 'Afternoon';
}?>


<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row['department'] ?></td>
<td><?php echo $row['status'] ?></td>
<td><select class="form-control" id="school" required>
<option value="" selected disabled>-Select School-</option>
<?php
include '../database/config.php';
$sql = "SELECT * FROM tbl_school WHERE status = 'Active' ORDER BY name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
print '<option value="'.$row['school_id'].'">'.$row['name'].'</option>';
}
} else {

}
?>

</td>
<?php
$no++;
}}?>

</tr>


</tbody>
</table>

最佳答案

是否包含“../database/config.php”;并包含“config.php”;来自不同的数据库源,如果是,您应该在两个不同的资源连接之间分开,或者正如@Sean所说,您应该更改 tbl_school 行和 tbl_department 行的变量名称

我创建了 $select_options 作为示例,用于保留 tbl_school 中的所有行,防止您查询每个 tbl_department 行:

<div class="table-responsive">
<?php


include '../database/config.php';
$sql = "SELECT * FROM tbl_school WHERE status = 'Active' ORDER BY name";
$result = $conn->query($sql);
$select_options = "";
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
$select_options .= '<option value="'.$row['school_id'].'">'.$row['name'].'</option>';
}
}
?>
<?php
include 'config.php';
$sql = "SELECT * FROM tbl_department";
$result = $conn->query($sql);

?>
<table>
<thead>
<tr>
<th>NO</th>
<th>Department</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<?php
if ($result->num_rows > 0) {
?>
<tbody>
<?php
$no = 1;
while($row = $result->fetch_assoc()) {
$session = $row['session'];
if ($session == "AM") {
$st = 'Morning';
}else{
$st = 'Afternoon';
}
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row['department'] ?></td>
<td><?php echo $row['status'] ?></td>
<td>
<select class="form-control" id="school" required>
<option value="" selected disabled>-Select School-</option>
<?php
echo $select_options;
?>
</select>
</td>
</tr>
<?php
$no++;
}
?>
</tbody>
<?php
}
?>
</table>
</div>

关于php - 从不同的表中获取并添加下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53491628/

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