php 从数据库表中填充选择下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/199597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate select drop down from a database table
提问by Brad
I have a table ("venues") that stores all the possible venues a volunteer can work, each volunteer is assigned to work one venue each.
我有一张桌子(“场地”),其中存储了志愿者可以工作的所有可能的场地,每个志愿者都被分配到一个场地工作。
I want to create a select drop down from the venues table.
我想从场地表中创建一个选择下拉列表。
Right now I can display the venue each volunteer is assigned, but I want it to display the drop down box, with the venue already selected in the list.
现在我可以显示每个志愿者被分配的地点,但我希望它显示下拉框,地点已经在列表中选择。
<form action="upd.php?id=7">
<select name="venue_id">
<?php //some sort of loop goes here
print '<option value="'.$row['venue_id'].'">'.$row['venue_name'].'</option>';
//end loop here ?>
</select>
<input type="submit" value="submit" name="submit">
</form>
For example, volunteer with the id of 7, is assigned to venue_id 4
比如id为7的志愿者,分配到venue_id为4
<form action="upd.php?id=7">
<select name="venue_id">
<option value="1">Bagpipe Competition</option>
<option value="2">Band Assistance</option>
<option value="3">Beer/Wine Pouring</option>
<option value="4" selected>Brochure Distribution</option>
<option value="5">Childrens Area</option>
<option value="6">Cleanup</option>
<option value="7">Cultural Center Display</option>
<option value="8">Festival Merch</option>
</select>
<input type="submit" value="submit" name="submit">
</form>
Brochure Distribution option will already be selected when it displays the drop down list, because in the volunteers_2009 table, column venue_id is 4.
I know it will take a form of a for or while loop to pull the list of venues from the venues table
我知道它将采用 for 或 while 循环的形式从场地表中拉出场地列表
My query is:
我的查询是:
$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";
How do I populate the select drop down box with the venues (volunteers_2009.venue_id, venues.id) from the venues table and have it pre-select the venue in the list?
如何填充选择下拉框与场馆(volunteers_2009.venue_id,venues.id从场地表),并已预先在列表中选择场地?
回答by
$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";
$res = mysql_query($query);
echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
echo "<option value = '{$row['venue_id']}'";
if ($selected_venue_id == $row['venue_id'])
echo "selected = 'selected'";
echo ">{$row['venue_name']}</option>";
}
echo "</select>";
:)
:)
回答by Brad
assuming you have an array of venues...personally i don't like to mix the sql with other wizardry.
假设你有一系列场地......我个人不喜欢将 sql 与其他巫术混合使用。
function displayDropDown($items, $name, $label, $default='') {
if (count($items)) {
echo '<select name="' . $name . '">';
echo '<option value="">' . $label . '</option>';
echo '<option value="">----------</option>';
foreach($items as $item) {
$selected = ($item['id'] == $default) ? ' selected="selected" : '';
echo <option value="' . $item['id'] . '"' . $selected . '>' . $item['name'] . '</option>';
}
echo '</select>';
} else {
echo 'There are no venues';
}
}
回答by duc14s
<?php
$query = "SELECT * from blogcategory";
//$res = mysql_query($query);
$rows = $db->query($query);
echo "<select name = 'venue'>";
// while (($row = mysql_fetch_row($res)) != null)
while ($record = $db->fetch_array($rows))
{
echo "<option value = '{$record['CategoryId']}'";
if ($CategoryId == $record['CategoryId'])
echo "selected = 'selected'";
echo ">{$record['CategoryName']}</option>";
}
echo "</select>";
?>
回答by Rishi
<!DOCTYPE html>
<html>
<head>
<title>table binding</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="mydiv" style="width:100px;height:100px;background-color:yellow">
<select id="myselect"></select>
</div>
</body>
</html>
<?php
include('dbconnection.php');
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn,$sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
}
while ($row = mysqli_fetch_row($result)) {
echo "<script>
var z = document.createElement('option');
z.setAttribute('value', '".$row[0]."');
var t = document.createTextNode('".$row[0]."');
z.appendChild(t);
document.getElementById('myselect').appendChild(z);</script>";
}
?>

