从下拉菜单中的SQL数据库显示数据 [英] Display data from SQL database in a drop down menu
问题描述
基本上我要完成的是这样的:
查询我的数据库并将客户端的所有名称存储到变量中。假设数据库中有5个名字,我需要将这5个名称存储在一个变量中。然后,对于我的下拉菜单,通常我把文本放在这样:<选项>单层
但是如何让这5个名字出现在下拉列表中?
下面是一个简单的伪脚本,从数据库中选择信息并输出一个选择下拉框。您将需要使用您正在使用的任何DB扩展名替换 * _ fetch_array
,而 $ row ['Value']
和 $ row ['DisplayValue'] 与DB Schema中的相应字段名称。
< select name ='iAmASelect'id ='iAmaASelect'>
<?php
$ DB_Rows = / *从数据库获取数据* /;
while($ row = * _fetch_array($ DB_Rows))
echo(< option value ='。$ row ['Value']。''>$ row ['DisplayValue ']。< / option>);
?>
< / select>
select将提交 $ row ['Value']
到表单处理程序,同时在下拉列表中向用户显示 $ row ['DisplayValue']
。
I have a database with names stored. I have my query of the database working, but lets say I have 5 names that I want to display in a drop down menu. How do I make the default text in the drop down menu display those 5 names?
Basically what I am trying to accomplish is this:
Query my database and store all the names of clients to a variable. Say there are 5 names in the database, I need those 5 names to be stored in a variable. And then for my drop down menu, normally I put the text in like this: < option>Single Floor< /option>
But how do I get those 5 names to appear in the drop down list?
Below is a simple pseudo-script that selects information from a database and outputs a select drop down box. You will need to replace *_fetch_array
with whatever DB Extension you are using, and $row['Value']
and $row['DisplayValue']
with the appropriate field names from your DB Schema.
<select name = 'iAmASelect' id = 'iAmaASelect'>
<?php
$DB_Rows = /* fetch data from database */;
while($row = *_fetch_array($DB_Rows))
echo("<option value = '" . $row['Value'] . "'>" . $row['DisplayValue'] . "</option>");
?>
</select>
The select will submit $row['Value']
to the form handler, while displaying $row['DisplayValue']
to the user in the drop down list.
这篇关于从下拉菜单中的SQL数据库显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!