从其他表格中获取并添加下拉菜单

时间:2018-11-27 01:50:51

标签: php mysql

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

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

3 个答案:

答案 0 :(得分:0)

是否包含“ ../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>

答案 1 :(得分:0)

我想使用此行隐藏一些ID

while($ row = $ result-> fetch_assoc()){         $ select_options。=''。$ row ['name']。'';       }     }     ?>

我可以在该语句之后添加它,还是必须在添加它的位置

答案 2 :(得分:0)

要覆盖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'];

              }
        }