How can PHP be used to filter search results based on multiple criteria such as brand, model, and color?
To filter search results based on multiple criteria such as brand, model, and color in PHP, you can use a combination of SQL queries and conditional statements. You can dynamically construct a SQL query based on the selected criteria and then fetch the results from the database.
// Assuming you have variables $brand, $model, and $color containing the selected criteria
// Construct the base SQL query
$query = "SELECT * FROM products WHERE 1=1";
// Add conditions based on selected criteria
if (!empty($brand)) {
$query .= " AND brand = '$brand'";
}
if (!empty($model)) {
$query .= " AND model = '$model'";
}
if (!empty($color)) {
$query .= " AND color = '$color'";
}
// Execute the query and fetch results
$result = mysqli_query($conn, $query);
// Loop through the results and display them
while ($row = mysqli_fetch_assoc($result)) {
// Display product information
}