Fetch and add drop down menu from different table












0















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>









share|improve this question

























  • You are using the same variable names for both loops. This causes them to overwrite each other. Change the variable names in the 2nd Loop. Also, no need to include the config file again.

    – Sean
    Nov 27 '18 at 2:52
















0















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>









share|improve this question

























  • You are using the same variable names for both loops. This causes them to overwrite each other. Change the variable names in the 2nd Loop. Also, no need to include the config file again.

    – Sean
    Nov 27 '18 at 2:52














0












0








0








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>









share|improve this question
















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>






php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 2:16









Pang

6,9281664102




6,9281664102










asked Nov 27 '18 at 1:50









abbaganaabbagana

11




11













  • You are using the same variable names for both loops. This causes them to overwrite each other. Change the variable names in the 2nd Loop. Also, no need to include the config file again.

    – Sean
    Nov 27 '18 at 2:52



















  • You are using the same variable names for both loops. This causes them to overwrite each other. Change the variable names in the 2nd Loop. Also, no need to include the config file again.

    – Sean
    Nov 27 '18 at 2:52

















You are using the same variable names for both loops. This causes them to overwrite each other. Change the variable names in the 2nd Loop. Also, no need to include the config file again.

– Sean
Nov 27 '18 at 2:52





You are using the same variable names for both loops. This causes them to overwrite each other. Change the variable names in the 2nd Loop. Also, no need to include the config file again.

– Sean
Nov 27 '18 at 2:52












3 Answers
3






active

oldest

votes


















0














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>





share|improve this answer
























  • it works thanks

    – abbagana
    Jan 29 at 11:35



















0














i want to covert some id using this line



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



can i add it after that statement or it must be where you added it






share|improve this answer































    0














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

    }
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491628%2ffetch-and-add-drop-down-menu-from-different-table%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      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>





      share|improve this answer
























      • it works thanks

        – abbagana
        Jan 29 at 11:35
















      0














      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>





      share|improve this answer
























      • it works thanks

        – abbagana
        Jan 29 at 11:35














      0












      0








      0







      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>





      share|improve this answer













      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>






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 27 '18 at 4:58









      Imran NababanImran Nababan

      2614




      2614













      • it works thanks

        – abbagana
        Jan 29 at 11:35



















      • it works thanks

        – abbagana
        Jan 29 at 11:35

















      it works thanks

      – abbagana
      Jan 29 at 11:35





      it works thanks

      – abbagana
      Jan 29 at 11:35













      0














      i want to covert some id using this line



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



      can i add it after that statement or it must be where you added it






      share|improve this answer




























        0














        i want to covert some id using this line



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



        can i add it after that statement or it must be where you added it






        share|improve this answer


























          0












          0








          0







          i want to covert some id using this line



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



          can i add it after that statement or it must be where you added it






          share|improve this answer













          i want to covert some id using this line



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



          can i add it after that statement or it must be where you added it







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 5 '18 at 14:03









          abbaganaabbagana

          11




          11























              0














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

              }
              }





              share|improve this answer




























                0














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

                }
                }





                share|improve this answer


























                  0












                  0








                  0







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

                  }
                  }





                  share|improve this answer













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

                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 29 at 14:19









                  Imran NababanImran Nababan

                  2614




                  2614






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491628%2ffetch-and-add-drop-down-menu-from-different-table%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)