从不同的表中获取和添加下拉菜单

qlvxas9a  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(339)

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

<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>
ghhkc1vu

ghhkc1vu1#

要覆盖id,您可以在$select\u options收集数据的地方获取它,也可以生成另一个值,比如说它是$collected\u id

$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'];

              }
        }
lnlaulya

lnlaulya2#

是否包含“../database/config.php”;并包括'config.php';从不同的数据库源,如果是你应该分开两个不同的资源连接,或者如@sean所说你应该改变tbl\u school rows和tbl\u department rows的变量名
我创建了$select\u options作为示例,用于保留tbl\u school的所有行,以防止您查询每个tbl\u部门行:

<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>

相关问题