如何设置 selected populated select 选项

How to set selected populated select option

我想问一些关于我的代码的问题。在MySQL数据库中,有两个table,分别是table'Team'和table'Borrower'.

下面是table'Team'

的图片

下面是table'Borrower'

的图片

在 PHP 页面,用户可以编辑任何用户的详细信息,包括他们的团队。从上面的table来看,南龙team_id是3,也就是团队'burner'。但是在页面上,我想如何显示在 Team select 选项,将列出所有在 table 'Team' 但 selected 是 'burner' 的团队,因为这是 Namron 目前的团队。

下面是我当前的代码。

borrower_details.php

<?php

    $badgeid = $_POST['badgeid'];

    $sql = "SELECT * 
            FROM ets_borrower 
                INNER JOIN ets_team ON ets_borrower.team_id = ets_team.team_id 
            WHERE ets_borrower.status_id = 1 
              AND ets_borrower.badgeid = :badgeid 
            ORDER BY ets_borrower.fullname ASC";
    
    $query = $conn->prepare($sql);
    $query->execute(array(':badgeid' => $badgeid));
    while($row = $query->fetch(PDO::FETCH_ASSOC)){
        $badgeid = $row["badgeid"];
        $fullname = $row["fullname"];
        $team_id = $row["team_id"];
        $team_name = $row["team_name"];
    }

    $sql2 = "SELECT * FROM ets_team WHERE status = 1";
    $query2 = $conn->prepare($sql2);
    $query2->execute();

    while($row2 = $query2->fetch(PDO::FETCH_ASSOC)){
    
        $team_id2 = $row2["team_id"];
        $team_name2 = $row2["team_name"];

?>

    <div class="form-group row">
        <div class="col-xs-3">
            <label for="example-search-input">Badge ID</label>
        </div>
        <div class="col-xs-9">
            <input class="form-control" type="text" name="badgeid" value="<?php echo $badgeid; ?>" readonly>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-xs-3">
            <label for="example-search-input">Name</label>
        </div>
        <div class="col-xs-9">
            <input class="form-control" type="text" name="fullname" value="<?php echo strtoupper($fullname); ?>" readonly>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-xs-3">
            <label for="example-search-input">Team</label>
        </div>
        <div class="col-xs-9">
            <select class="form-control" id="exampleFormControlSelect1" name="team_id">
                <option value="<?php echo $team_id;?>" <?php echo $team_id2 == $team_id? 'selected': '';?> ><?php echo $team_name2;?></option>
            </select>
        </div>
    </div>

将您的 while 循环移动到 select 标记下方。

<select class="form-control" id="exampleFormControlSelect1" name="team_id">
<?php
while($row2 = $query2->fetch(PDO::FETCH_ASSOC)){
        $team_id2 = $row2["team_id"];
        $team_name2 = $row2["team_name"];
?>
<option value="<?php echo $team_id;?>" <?php echo $team_id2 == $team_id? 'selected': '';?> ><?php echo $team_name2;?></option>
<?php }
?>
</select>

详见代码注释

基本上,您必须将应该加载下拉列表的 while 循环移动到仅要加载选项列表的位置

$badgeid = $_POST['badgeid'];

$sql = "SELECT * 
        FROM ets_borrower 
            INNER JOIN ets_team ON ets_borrower.team_id = ets_team.team_id 
        WHERE ets_borrower.status_id = 1 
          AND ets_borrower.badgeid = :badgeid 
        ORDER BY ets_borrower.fullname ASC";

$query = $conn->prepare($sql);
$query->execute(array(':badgeid' => $badgeid));

// while loop not require, only one row can be returned
$row = $query->fetch(PDO::FETCH_ASSOC);
$badgeid = $row["badgeid"];
$fullname = $row["fullname"];
$team_id = $row["team_id"];
$team_name = $row["team_name"];

$sql2 = "SELECT * FROM ets_team WHERE status = 1";
$query2 = $conn->prepare($sql2);
$query2->execute();
// while loop moved down to load just the dropdown
?>

<div class="form-group row">
    <div class="col-xs-3">
        <label for="example-search-input">Badge ID</label>
    </div>
    <div class="col-xs-9">
        <input class="form-control" type="text" name="badgeid" value="<?php echo $badgeid; ?>" readonly>
    </div>
</div>
<div class="form-group row">
    <div class="col-xs-3">
        <label for="example-search-input">Name</label>
    </div>
    <div class="col-xs-9">
        <input class="form-control" type="text" name="fullname" value="<?php echo strtoupper($fullname); ?>" readonly>
    </div>
</div>
<div class="form-group row">
    <div class="col-xs-3">
        <label for="example-search-input">Team</label>
    </div>
    <div class="col-xs-9">
        <select class="form-control" id="exampleFormControlSelect1" name="team_id">
<?php
while($row2 = $query2->fetch(PDO::FETCH_ASSOC)) :

    $sel = $team_id2 == $team_id? 'selected' : '';
    echo "<option value='$team_id' $sel>$row2[team_name]</option>";
endwhile;
?>
        </select>
    </div>
</div>