How can dropdown menus in PHP be used to filter and display specific rows in a table based on user selection?

Dropdown menus in PHP can be used to filter and display specific rows in a table based on user selection by utilizing a form with a dropdown menu that contains filtering options. When the user selects an option and submits the form, the PHP code can retrieve the selected value and use it to query the database for specific rows that match the selected criteria. The retrieved data can then be displayed in a table on the webpage.

<?php
// Assuming a database connection is already established
// Retrieve the selected option from the form submission
if(isset($_POST['filter'])){
    $selectedOption = $_POST['filter'];
    
    // Query the database based on the selected option
    $query = "SELECT * FROM table_name WHERE column_name = '$selectedOption'";
    $result = mysqli_query($conn, $query);
    
    // Display the retrieved data in a table
    echo "<table>";
    while($row = mysqli_fetch_assoc($result)){
        echo "<tr>";
        echo "<td>".$row['column1']."</td>";
        echo "<td>".$row['column2']."</td>";
        // Add more columns as needed
        echo "</tr>";
    }
    echo "</table>";
}
?>

<!-- Create a form with a dropdown menu for filtering -->
<form method="post">
    <select name="filter">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <!-- Add more options as needed -->
    </select>
    <input type="submit" value="Filter">
</form>