Filter by search term
unknown
php
a year ago
8.1 kB
5
Indexable
<?php include 'database/db_connection.php'; $recordsPerPage = 4; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $offset = ($page - 1) * $recordsPerPage; // Check if the form is submitted or a hotel is searched if ($_SERVER["REQUEST_METHOD"] == "POST" || isset($_GET['hotel'])) { $hotel = isset($_REQUEST["hotel"]) ? $_REQUEST["hotel"] : ''; $minLength = 2; // Minimum number of characters required for the search if (strlen($hotel) < $minLength) { echo '<div class="alert alert-warning" role="alert">No search terms found for the specified search: ' . $hotel . '</div>'; exit; } // Fetch filtered data from the database with search term $searchFilter = "AND (hotel LIKE '%$hotel%' OR facilities LIKE '%$hotel%')"; } else { $searchFilter = ''; // Default empty search filter } // Extract and sanitize filter parameters $tvFilter = isset($_GET['tv']) && $_GET['tv'] == '1' ? 'AND facilities LIKE "%TV%"' : ''; $wifiFilter = isset($_GET['wifi']) && $_GET['wifi'] == '1' ? 'AND facilities LIKE "%Wi-Fi%"' : ''; $acFilter = isset($_GET['ac']) && $_GET['ac'] == '1' ? 'AND facilities LIKE "%AC%"' : ''; $parkFilter = isset($_GET['parking']) && $_GET['parking'] == '1' ? 'AND facilities LIKE "%Parking%"' : ''; $kitchenFilter = isset($_GET['kitchen']) && $_GET['kitchen'] == '1' ? 'AND facilities LIKE "%Kitchen%"' : ''; $ratingFilter = ''; if (isset($_GET['rating']) && is_array($_GET['rating'])) { $ratings = array_map('intval', $_GET['rating']); $ratingFilter = "AND score >= " . min($ratings); } // Extract and sanitize adult and children filters $adultFilter = isset($_GET['adult']) ? 'AND adult >= ' . intval($_GET['adult']) : ''; $childrenFilter = isset($_GET['children']) ? 'AND children >= ' . intval($_GET['children']) : ''; // Fetch filtered data from the database $sql = "SELECT DISTINCT hotel, price, score, room, bed, adult, children, facilities, image_url FROM ( SELECT * FROM egypt UNION ALL SELECT * FROM croatia UNION ALL SELECT * FROM bali UNION ALL SELECT * FROM caribbean UNION ALL SELECT * FROM china UNION ALL SELECT * FROM spain ) AS combined_data WHERE 1=1 $tvFilter $wifiFilter $acFilter $parkFilter $kitchenFilter $ratingFilter $adultFilter $childrenFilter $searchFilter LIMIT $offset, $recordsPerPage"; $result = $conn->query($sql); if ($result->num_rows > 0) { // If a search is performed, display the search term if ($_SERVER["REQUEST_METHOD"] == "POST" || isset($_GET['hotel'])) { echo '<div class="title px-4">'; echo '<h2 class="fw-bold h-font text-center">You searched for: ' . htmlspecialchars($hotel) . '</h2>'; echo '</div>'; } while ($row = $result->fetch_assoc()) { $decodedPrice = json_decode('"' . $row['price'] . '"'); $numericValue = filter_var($decodedPrice, FILTER_SANITIZE_NUMBER_INT); // Use Bootstrap classes for styling the card echo '<div class="card w-100 mb-3">'; echo '<div class="row g-0 p-3">'; echo '<div class="col-md-4">'; echo '<img src="' . $row['image_url'] . '" class="img-fluid" alt="' . $row['hotel'] . '">'; echo '</div>'; echo '<div class="col-md-4">'; echo '<h5 class="card-title">' . $row['hotel'] . '</h5>'; echo '<div class="spans">'; echo '<span class="badge bg-secondary">Rooms ' . $row['room'] . '</span>'; echo '<span class="badge bg-secondary">Beds ' . $row['bed'] . '</span>'; echo '</div>'; echo '<div class="spans">'; echo '<span class="badge bg-secondary">Adult ' . $row['adult'] . '</span>'; if ($row['children'] > 0) { echo '<span class="badge bg-secondary">Children ' . $row['children'] . '</span>'; } echo '</div>'; echo '<div class="facilities">'; $facilitiesArray = explode(', ', $row['facilities']); foreach ($facilitiesArray as $facility) { echo '<span class="badge bg-secondary">' . $facility . '</span>'; } echo '</div>'; echo '</div>'; echo '<div class="col-md-4">'; echo '<div class="card-body">'; echo '<p class="price">' . $numericValue . " €" . '</p>'; echo '<div class="rating_bg">'; echo '<p class="rating">'; // Display score without decimal point if it's 10 or greater if ($row['score'] >= 10) { echo floor($row['score']); } else { echo $row['score']; } echo '</p>'; echo '</div>'; echo '</div>'; echo '<form action="booking.php" method="POST">'; echo '<input type="hidden" name="hotel_id" value="' . htmlspecialchars($row['hotel']) . '">'; echo '<button type="submit" class="btn btn">Book now</button>'; echo '</form>'; echo '</div>'; echo '</div>'; echo '</div>'; } // Pagination links with combined table filter $sqlCount = "SELECT COUNT(*) AS total FROM ( SELECT DISTINCT hotel FROM egypt WHERE 1=1 $tvFilter $wifiFilter $acFilter $parkFilter $kitchenFilter $ratingFilter $adultFilter $childrenFilter $searchFilter UNION ALL SELECT DISTINCT hotel FROM croatia WHERE 1=1 $tvFilter $wifiFilter $acFilter $parkFilter $kitchenFilter $ratingFilter $adultFilter $childrenFilter $searchFilter UNION ALL SELECT DISTINCT hotel FROM bali WHERE 1=1 $tvFilter $wifiFilter $acFilter $parkFilter $kitchenFilter $ratingFilter $adultFilter $childrenFilter $searchFilter UNION ALL SELECT DISTINCT hotel FROM caribbean WHERE 1=1 $tvFilter $wifiFilter $acFilter $parkFilter $kitchenFilter $ratingFilter $adultFilter $childrenFilter $searchFilter UNION ALL SELECT DISTINCT hotel FROM china WHERE 1=1 $tvFilter $wifiFilter $acFilter $parkFilter $kitchenFilter $ratingFilter $adultFilter $childrenFilter $searchFilter UNION ALL SELECT DISTINCT hotel FROM spain WHERE 1=1 $tvFilter $wifiFilter $acFilter $parkFilter $kitchenFilter $ratingFilter $adultFilter $childrenFilter $searchFilter ) AS combined_hotels"; $resultCount = $conn->query($sqlCount); $rowCount = $resultCount->fetch_assoc(); $totalPages = ceil($rowCount['total'] / $recordsPerPage); echo '<nav aria-label="Page navigation">'; echo '<ul class="pagination">'; for ($i = 1; $i <= $totalPages; $i++) { echo '<li class="page-item ' . ($page == $i ? 'active' : '') . '">'; // Maintain other filters in pagination links $filters = array( 'tv' => isset($_GET['tv']) ? $_GET['tv'] : '', 'wifi' => isset($_GET['wifi']) ? $_GET['wifi'] : '', 'ac' => isset($_GET['ac']) ? $_GET['ac'] : '', 'parking' => isset($_GET['parking']) ? $_GET['parking'] : '', 'kitchen' => isset($_GET['kitchen']) ? $_GET['kitchen'] : '', 'rating' => isset($_GET['rating']) ? $_GET['rating'] : array(), 'adult' => isset($_GET['adult']) ? $_GET['adult'] : '', // Include adult filter 'children' => isset($_GET['children']) ? $_GET['children'] : '', // Include children filter ); // Add hotel search term only if it's set if (isset($hotel)) { $filters['hotel'] = $hotel; } echo '<a class="page-link" href="?page=' . $i . '&' . http_build_query($filters) . '" style="' . ($page == $i ? 'background-color: darkblue; color: white; border: 0;' : '') . '">' . $i . '</a>'; echo '</li>'; } echo '</ul>'; echo '</nav>'; } else { echo '<div class="alert alert-warning mt-5" role="alert">No search terms found for: ' . '<b>' . $hotel . '</b>' . '</div>'; } $conn->close(); ?>
Editor is loading...
Leave a Comment