【发布时间】:2014-06-26 08:59:59
【问题描述】:
我曾尝试搜索 StackOverlow(和 google),但找不到我要查找的内容。
基本上我需要从 mysql 表中获取 id 和文本列表。
表:
ID:1 - 文本:Title1 ID:2 - 文本:Title2 等等
但我希望它用文本填充下拉列表,当我在下拉列表中选择一个项目时,它会给我该文本的 ID(字符串或 int)。
所以如果我选择 Title2,它应该给我 2
<?
include 'db.php';
$query="SELECT topic_id,topic FROM help_topic WHERE isactive=1 ORDER BY topic";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
$result = mysql_query ($query);
echo "<select name=category value='' id=category></option>";
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[topic_id]>$nt[topic]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
【问题讨论】:
-
所以,
<option value="[id]">[title]</option><select> 中,完成! -
当您说“下拉菜单”时,您是指 SELECT 菜单(正如 Jack 所说)还是使用 HTML UL 和 CSS 的下拉菜单?
-
这是一个选择菜单:-)
标签: php mysql arrays drop-down-menu