php 如何在php中显示数据库中下拉列表的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10532896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:24:41  来源:igfitidea点击:

How to show selected value of dropdown list from database in php

php

提问by Gabriel Alain

How do I show the selected value of a dropdown list from my mysql database. The dropdown list is dependent to my Categorydropdown list. These are the codes:

如何从我的 mysql 数据库显示下拉列表的选定值。下拉列表取决于我的Category下拉列表。这些是代码:

<?php $id = $_GET["id"];
if(!$pupcon){
    die(mysql_error());
}
mysql_select_db($database_pupcon, $pupcon);

$getDropdown2 = mysql_query("select * from tblitemname where CategoryID = $id");
    while($row = mysql_fetch_array($getDropdown2)){
        echo "<option value=\"".$row["ItemID"]."\">".$row["Item_Name"]."</option>";
    }   ?>

Here are the codes for the first dropdown list (Category) which populates the Item Namedropdown.

以下是Category填充下拉列表的第一个下拉列表 ( )的代码Item Name

<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
                                <?php do {  ?>
                                <option value="<?php echo $row_Recordset1['CategoryID']?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['CategoryName']?></option>
                                <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); $rows = mysql_num_rows($Recordset1); if($rows > 0) {
  mysql_data_seek($Recordset1, 0);
  $row_Recordset1 = mysql_fetch_assoc($Recordset1);}?>
                            </select>

回答by Aaron W.

While you're listing out the drop down options, you can check to see if the current ItemIDmatches the passed idvalue. If it matches, throw a selected="selected"in there. Try:

当您列出下拉选项时,您可以检查当前是否ItemID与传递的id值匹配。如果匹配,selected="selected"在那里扔一个。尝试:

$selected = ($row['ItemID'] == $id);
echo "<option value=\"".$row["ItemID"]."\" ".($selected ? " selected=\"selected\"":"").">".$row["Item_Name"]."</option>";

EDIT

编辑

I tried to clean up the code some...not sure why you're using a do...whilebecause $row_Recordset1wouldn't be available on the first iteration.

我试图清理代码一些......不知道你为什么使用 ado...while因为$row_Recordset1在第一次迭代时不可用。

<select name="Category" id="Category" class="field select large" onChange="loadXMLDoc(this.value);">
    <?php 
    while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {  
    ?>
    <option value="<?php echo $row_Recordset1['CategoryID']; ?>"<?php if (!(strcmp($row_Recordset1['CategoryID'], $row_editRecordset['CategoryID']))) { echo " selected=\"selected\""; } ?>>
    <?php echo $row_Recordset1['CategoryName']; ?>
    </option>
    <?php 
    }
    ?>
</select>

回答by Rohit Kumar Choudhary

you can use this code inside while

你可以在里面使用这个代码

$selected=$row["ItemID"]==$id ?'Selected':'';
echo "<option value=\"".$row["ItemID"]."\" {$selected} >".$row["Item_Name"]."</option>";;