【问题标题】:PHP Dropdown of all records from database + select current set record数据库中所有记录的PHP下拉列表+选择当前设置的记录
【发布时间】:2012-09-05 08:38:39
【问题描述】:

我有一个组名列表的 PHP 下拉列表(连同 id,因此可以更新)。在此 FORM 页面中,您可以通过从来自数据库的下拉列表中选择可能性来更改为项目指定的组名。我下面的代码有效,但一定有更好的方法,因为我得到了第一个字段作为当前设置,然后是所有的可能性,所以我得到了两次这个记录。

示例:
- 键盘(当前设置)
- 扬声器(可以选择,直接来自星展银行)
- Midi 控制器(可以选择,直接来自 DBS)
- 键盘(可以选择,直接来自 DBS)
- 鼓组(可以选择,直接来自 DBS)

如你所见,我再次获得了当前设置的记录。

我的代码:

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>";

【问题讨论】:

  • 与其运行两个查询,不如运行一个查询?当当前记录 itemgroupname 匹配 $itemgroupname 时,您只需为该记录回显一个“已选择”。

标签: php html select drop-down-menu menu


【解决方案1】:

有两种方法可以实现您的目标:

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) 在自然位置显示所选项目

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

【讨论】:

  • 他说什么!哈哈,我正要发布与这两个选项完全相同的答案。
【解决方案2】:

好的

在您的代码中。 而不是在顶部输出您选择的值,而是以正确的方式进行:)

选择您当前的项目(例如记下 itemgroupid)

然后在你的输出中

while($nt=mysql_fetch_array($itemgroupqueryresult)){ 
    echo "<option ";
    if ($savedid==$nt[itemgroupid]) echo "selected ";
    echo "$nt[itemgroupid]>$nt[itemgroupname]</option>"; 
} 
echo "</select>"; 

这将产生 $savedid=1

<option value=0>group 0</option>
<option selected value=1>group 1</option>
<option value=2>group 2</option>

【讨论】:

  • 我是个傻瓜哈哈。并且 ($savedid==value=$nt...) 由于 value 后的 '=' 给了我错误。
【解决方案3】:

先将默认选中的记录添加到空数组中

toDisplay = array('selected_record');

然后,使用您的 sql 从数据库中获取数据并将其附加到该数组中。

稍后在其上运行array_unique,最后使用循环创建 html 输出字符串,方法与现在相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2016-07-02
    相关资源
    最近更新 更多