Search code examples
phpmysqlselect-options

How to set selected populated select option


I want to ask something regarding my codes. In MySQL database, there are two tables which are table 'Team' and table 'Borrower'.

Below is the image of table 'Team'

enter image description here

And below is the image of table 'Borrower'

enter image description here

AT PHP page, User can edit info of any User's details including their team. From the table above, Namron team_id is 3 which is team 'burner'. But at the page, how I want to display at Team select option, will list all team that have at table 'Team' but selected is 'burner' since this is current team for Namron.

Below is my current code.

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>

Solution

  • Move your while loop below select tag.

    <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>