[英]Fetch and add drop down menu from different table
I have a start connection already I want to add another drop down menu that will fetch data from different table and assign it to all users when I try to works but it only show one user while there are many inside the database. 我已经有一个启动连接,我想添加另一个下拉菜单,当我尝试工作时,它将从不同的表中获取数据并将其分配给所有用户,但是它仅显示一个用户,而数据库中却有许多用户。 When I remove the drop down menu, it shows all the users from the database.
当我删除下拉菜单时,它将显示数据库中的所有用户。
<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>
is it include '../database/config.php'; 是否包含“ ../database/config.php”; and include 'config.php';
并包含“ config.php”; from different database source, if yes you should separate between two of different resource connection, or as @Sean said you should change the variable name of tbl_school rows and tbl_department rows
来自不同的数据库源,如果是,则应在两个不同的资源连接之间进行分隔,或者如@Sean所述,应更改tbl_school行和tbl_department行的变量名称
I've created $select_options as sample for hold all rows from tbl_school prevent you to query of each tbl_department rows: 我创建了$ 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>
To cover id you can get it in where $select_options collected data, or you can make another value, let say it was $collected_ids 要覆盖id,可以在$ select_options收集数据的位置获取它,或者可以设置另一个值,假设它是$ collected_ids
$collected_ids = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$select_options .= '<option value="'.$row['school_id'].'">'.$row['name'].'</option>';
$collected_ids[] = $row['school_id'];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.