[英]PHP Dropdown of all records from database + select current set record
I have a PHP dropdown of a list of groupnames (together with id, so it can be updated). 我有一个PHP下拉列表,列出了组名列表(连同ID,因此可以更新)。 In this FORM page you can change the groupname specified for an item by choosing possibilities from the dropdown coming out from the database. 在此FORM页面中,您可以通过从数据库中选择下拉菜单中的可能性来更改为项目指定的组名。 My code below works, but there must be a better way, because I get the first field as the currently set, and then all the possibilities, so I get this record twice. 我下面的代码可以工作,但是必须有一种更好的方法,因为我将第一个字段作为当前设置,然后将所有可能的字段作为字段,因此我两次获得了该记录。
Example: 例:
- Keyboard (Currently set) -键盘(当前设置)
- Speakers (Possible to choose, straight from DBS) -扬声器(可以选择,直接从DBS选择)
- Midi Controllers (Possible to choose, straight from DBS) -Midi控制器(可以直接从DBS选择)
- Keyboard (Possible to choose, straight from DBS) -键盘(可以直接从DBS选择)
- Drum set (Possible to choose, straight from DBS) -鼓组(可以直接从DBS选择)
As you see I get the currently set record again. 如您所见,我再次获得了当前设定的记录。
My code: 我的代码:
echo "<select name='itemgroupid'>";
// CHOOSE CURRENT SET RECORD AS SELECTED ITEM
echo "<option value='" . $itemgroupid . "'>";
$selected="
SELECT item.itemid, itemgroup.itemgroupname, itemgroup.itemgroupid
FROM item, itemgroup
WHERE item.itemid=$itemid";
$selectedresult=mysql_query($query) or die("query fout " . mysql_error() );
while($record=mysql_fetch_array($selectedresult) ) {
echo "" . $itemgroupname . "</option>";
}
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option value=$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";
There are 2 ways to achieve what you're looking for: 有两种方法可以实现您想要的目标:
1) To show the selected item at the top of the dropdown 1)要在下拉菜单的顶部显示所选项目
echo "<select name='itemgroupid'>";
// CHOOSE CURRENT SET RECORD AS SELECTED ITEM
echo "<option value='" . $itemgroupid . "'>";
$selected="
SELECT item.itemid, itemgroup.itemgroupname, itemgroup.itemgroupid
FROM item, itemgroup
WHERE item.itemid=$itemid";
$selectedresult=mysql_query($query) or die("query fout " . mysql_error() );
while($record=mysql_fetch_array($selectedresult) ) {
echo "" . $itemgroupname . "</option>";
}
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup WHERE item.itemid != $itemid";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option value=$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";
2) Show the selected item in it natural place 2)在自然位置显示所选项目
echo "<select name='itemgroupid'>";
// QUERY TO SHOW ALL POSSIBLE CHOOSABLE RECORDS FROM DATABASE
$itemgroupquery="SELECT itemgroupname,itemgroupid FROM itemgroup";
$itemgroupqueryresult = mysql_query ($itemgroupquery);
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option value=$nt[itemgroupid]";
if( $itemid == $nt['itemgroupid'] ) echo ' selected="selected"';
echo ">$nt[itemgroupname]</option>";
}
echo "</select>";
HTH HTH
OK 好
In your code. 在您的代码中。 rather than output your selected value at the top, do it the proper way :) 而不是在顶部输出您选择的值,而是以正确的方式进行:)
Select your current item (make a note of the itemgroupid for example) 选择您当前的项目(例如记下itemgroupid)
then in your output 然后在您的输出中
while($nt=mysql_fetch_array($itemgroupqueryresult)){
echo "<option ";
if ($savedid==$nt[itemgroupid]) echo "selected ";
echo "$nt[itemgroupid]>$nt[itemgroupname]</option>";
}
echo "</select>";
This will produce with $savedid=1 这将产生$ savedid = 1
<option value=0>group 0</option>
<option selected value=1>group 1</option>
<option value=2>group 2</option>
Add the default selected record to a empty array first like 首先将默认选定记录添加到空数组中,例如
toDisplay = array('selected_record');
Then, get the data from the database using your sql and append it to this array. 然后,使用sql从数据库中获取数据并将其附加到此数组。
Later run a array_unique
on it and finally using a loop create the html output string, in the same way you are doing it now. 稍后在其上运行array_unique
,最后使用循环,以与现在相同的方式创建html输出字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.