如何在下拉列表中设置选定的选项

How to set selected option in dropdown

这将回显从数据库中获取的下拉值

<select name="PACKAGE_ID" id="PACKAGE_ID" ng-model="FormData.Phases" class="form-control" required>

<?php 
$result=mysqli_query($conn, "select * from unifi WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {
?>
    <option name="UNIFI" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>

<?php $result=mysqli_query($conn, "select * from streamyx WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
    <option name="STREAMYX" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>


<?php $result=mysqli_query($conn, "select * from webe WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
    <option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>

<?php $result=mysqli_query($conn, "select * from dome WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
    <option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>

下面是我如何根据工单 ID 从数据库中获取值

 $sql = "SELECT * FROM cusinfo WHERE TICKET_ID = '".$strid."' ";
 $query = mysqli_query($conn,$sql);
 $result=mysqli_fetch_array($query,MYSQLI_ASSOC);

我想回显为例如选择的值。<<< $result["PACKAGE_ID"]==$row[ "PACKAGE_NAME"] echo selected >>

因为您在后续查询中多次使用 $result 变量,所以您需要将 $result["PACKAGE_CATEGORY"] 保留为新变量,如下所示:

$selected=$result["PACKAGE_CATEGORY"];

您可以像这样编写内联条件语句:

echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"",($row[ "PACKAGE_NAME"]==$selected?" selected":""),">{$row["PACKAGE_NAME"]}</option>";

这意味着如果条件为真则 selected 将被回显,如果为假则将回显空字符串。

在多行上使用相同的技术将如下所示:

echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
    echo $row["PACKAGE_NAME"]==$selected?" selected":"";
echo ">{$row["PACKAGE_NAME"]}</option>";

如果您不想使用内联条件,这里是标准语法:

echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
    if($row["PACKAGE_NAME"]==$selected){
        echo " selected";
    }
echo ">{$row["PACKAGE_NAME"]}</option>";