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>
php mysql
add a comment |
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
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 toinclude
the config file again.
– Sean
Nov 27 '18 at 2:52
add a comment |
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
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
php mysql
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 toinclude
the config file again.
– Sean
Nov 27 '18 at 2:52
add a comment |
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 toinclude
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
add a comment |
3 Answers
3
active
oldest
votes
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>
it works thanks
– abbagana
Jan 29 at 11:35
add a comment |
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
add a comment |
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'];
}
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
it works thanks
– abbagana
Jan 29 at 11:35
add a comment |
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>
it works thanks
– abbagana
Jan 29 at 11:35
add a comment |
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>
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>
answered Nov 27 '18 at 4:58
Imran NababanImran Nababan
2614
2614
it works thanks
– abbagana
Jan 29 at 11:35
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Dec 5 '18 at 14:03
abbaganaabbagana
11
11
add a comment |
add a comment |
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'];
}
}
add a comment |
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'];
}
}
add a comment |
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'];
}
}
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'];
}
}
answered Jan 29 at 14:19
Imran NababanImran Nababan
2614
2614
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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