簡體   English   中英

從其他表格中獲取並添加下拉菜單

[英]Fetch and add drop down menu from different table

我已經有一個啟動連接,我想添加另一個下拉菜單,當我嘗試工作時,它將從不同的表中獲取數據並將其分配給所有用戶,但是它僅顯示一個用戶,而數據庫中卻有許多用戶。 當我刪除下拉菜單時,它將顯示數據庫中的所有用戶。

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

要覆蓋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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM