How can checkboxes be effectively used to narrow down search results in PHP?

Checkboxes can be effectively used to narrow down search results in PHP by allowing users to select specific criteria they are interested in. This can be achieved by setting up checkboxes for different filters such as price range, category, size, color, etc. When the form is submitted, PHP can process the selected checkboxes and use them as filters in the database query to fetch only the relevant search results.

// Assuming checkboxes for filtering by category and price range
$categoryFilter = isset($_POST['category']) ? $_POST['category'] : '';
$priceFilter = isset($_POST['price']) ? $_POST['price'] : '';

// Construct the SQL query based on selected checkboxes
$sql = "SELECT * FROM products WHERE 1=1";
if(!empty($categoryFilter)) {
    $sql .= " AND category IN ('" . implode("','", $categoryFilter) . "')";
}
if(!empty($priceFilter)) {
    $sql .= " AND price BETWEEN " . min($priceFilter) . " AND " . max($priceFilter);
}

// Execute the query and display search results
// Remember to sanitize user input to prevent SQL injection