1 | initial version |
To filter data through a dropdown in PHP, you can follow these steps:
Here is an example code snippet that demonstrates how to filter data through a dropdown in PHP:
// Set up a form with a dropdown that contains the filter options
echo '<form method="post">';
echo '<select name="filter">';
echo '<option value="">All</option>';
echo '<option value="option1">Option 1</option>';
echo '<option value="option2">Option 2</option>';
echo '</select>';
echo '<input type="submit" name="submit" value="Filter">';
echo '</form>';
// If the form is submitted, retrieve the selected filter value and construct a SQL query
if (isset($_POST['submit'])) {
$filter = $_POST['filter'];
$sql = "SELECT * FROM my_table";
if (!empty($filter)) {
$sql .= " WHERE column_name = '$filter'";
}
// Execute the SQL query and display the filtered data
// ...
}
Note that you should always use prepared statements with parameter binding to prevent SQL injection attacks when constructing SQL queries.