How can you pre-select an option in a dropdown menu based on a value stored in a database using PHP?

To pre-select an option in a dropdown menu based on a value stored in a database using PHP, you can fetch the value from the database and compare it with each option in the dropdown menu. If a match is found, you can add the 'selected' attribute to that option to pre-select it.

<select name="dropdown">
<?php
// Assume $selectedValue is the value retrieved from the database
$options = ['Option 1', 'Option 2', 'Option 3'];

foreach ($options as $option) {
    if ($option == $selectedValue) {
        echo "<option value='$option' selected>$option</option>";
    } else {
        echo "<option value='$option'>$option</option>";
    }
}
?>
</select>