-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.php
More file actions
116 lines (101 loc) · 3.31 KB
/
queries.php
File metadata and controls
116 lines (101 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
// Function to build the search query
function buildSearchQuery($conn, $filters, $results_per_page, $offset) {
// Extract filters
$search = $filters['search'] ?? '';
$country = $filters['country'] ?? '';
$city = $filters['city'] ?? '';
$manufacturer = $filters['manufacturer'] ?? '';
$tag = $filters['tag'] ?? ''; // Explicitly extract the 'tag' filter
// Base SQL query
$sql = "SELECT id, source_url, cam_url, cam_stream, ipwithport, latitude, longitude, country,
city, manufacturer, title_seo, state, zipcode, image_url_full, tag, view_count
FROM webcams
WHERE 1=1";
$params = [];
$types = '';
// Free-text search query
if ($search) {
$sql .= " AND (title_seo LIKE ? OR country LIKE ? OR manufacturer LIKE ? OR city LIKE ? OR state LIKE ? OR tag LIKE ? OR zipcode LIKE ?)";
$searchTerm = '%' . $search . '%';
$params[] = $searchTerm;
$params[] = $searchTerm;
$params[] = $searchTerm;
$params[] = $searchTerm;
$params[] = $searchTerm;
$params[] = $searchTerm;
$params[] = $searchTerm;
$types .= 'sssssss';
}
// Dropdown filters
if ($country) {
$sql .= " AND country = ?";
$params[] = $country;
$types .= 's';
}
if ($city) {
$sql .= " AND city = ?";
$params[] = $city;
$types .= 's';
}
if ($manufacturer) {
$sql .= " AND manufacturer = ?";
$params[] = $manufacturer;
$types .= 's';
}
// Handle 'tag' as a filter if it's present
if ($tag) {
$sql .= " AND tag = ?";
$params[] = $tag;
$types .= 's';
}
// Count total results for pagination
$count_sql = "SELECT COUNT(*) FROM (" . $sql . ") AS total_query";
$count_stmt = $conn->prepare($count_sql);
if ($params) {
$count_stmt->bind_param($types, ...$params);
}
$count_stmt->execute();
$count_stmt->bind_result($total_results);
$count_stmt->fetch();
$count_stmt->close();
// Add limit for pagination
$sql .= " LIMIT ?, ?";
$types .= 'ii';
$params[] = $offset;
$params[] = $results_per_page;
$stmt = $conn->prepare($sql);
if ($params) {
$stmt->bind_param($types, ...$params);
}
return ['stmt' => $stmt, 'total_results' => $total_results];
}
// Function to fetch distinct values from a column for dropdowns
function getDistinctValues($conn, $field) {
$sql = "SELECT DISTINCT $field FROM webcams WHERE $field IS NOT NULL AND $field != '' ORDER BY $field ASC";
$result = $conn->query($sql);
$values = [];
if ($result && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$values[] = $row[$field];
}
}
return $values;
}
// Fetch cities by country (for dynamic city dropdowns)
function getCitiesByCountry($conn, $country) {
$sql = "SELECT DISTINCT city FROM webcams WHERE country = ? AND city IS NOT NULL AND city != '' ORDER BY city ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $country);
$stmt->execute();
$result = $stmt->get_result();
$cities = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$cities[] = $row['city'];
}
}
$stmt->close();
return $cities;
}
?>