简体   繁体   English

PHP从数据库中删除所有记录,并选择当前设置的记录

[英]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.

相关问题 php如何从数据库中创建所有表的下拉选择输入? - php how to create a dropdown select input of all table from a database? php从数据库中选择记录 - php select records from database PHP MYSQL 如何使用当前表的记录 ID 显示和列出两个表中的所有记录? - PHP MYSQL how to show and list all records from two tables using current table's record id? 选择上个月的所有记录,但选择当月的受限记录 - to select all records from the last month but restricted records in the current month 如何在 select 下拉列表中显示 PHP 数据库记录? - How to display PHP database records in select dropdown list? 如何从下拉列表中选择mysql数据库表以插入记录 - How to select mysql database table from a dropdown list to insert record 从Mysql Database Timestamp Laravel中仅选择当前周的记录 - Select only records of the current week from Mysql Database Timestamp Laravel 如何使用 PHP 从下拉列表中的数据库中加载记录 - How to load records from a database in a dropdown using PHP 从数据库填充的下拉列表未使用PHP记录所选选项 - Dropdown populated from database doesn't record selected option with PHP Select 所有记录仅显示在 jquery 数据表分页中的当前页面记录中? - Select all record only displaying in current page records in jquery datatable pagination?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM