简体   繁体   中英

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'; and include '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

I've created $select_options as sample for hold all rows from tbl_school prevent you to query of each tbl_department rows:

<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

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

              }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM