0

如何从我的 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>";
    }   ?>

以下是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>
4

2 回答 2

2

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

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

编辑

我试图清理一些代码......不知道你为什么使用 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>
于 2012-05-10T11:22:10.287 回答
0

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

$selected=$row["ItemID"]==$id ?'Selected':'';
echo "<option value=\"".$row["ItemID"]."\" {$selected} >".$row["Item_Name"]."</option>";;
于 2012-05-10T11:22:25.813 回答