-
Notifications
You must be signed in to change notification settings - Fork 0
/
explore.php
290 lines (237 loc) · 10.5 KB
/
explore.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php include 'includes/config.php' ?>
<?php include 'includes/head.php' ?>
<script src="js/school_redirect.js"></script>
<style>
.page-layout {
display: flex;
flex-wrap: nowrap;
justify-content: space-between;
padding: 20px;
}
.filter-bar {
flex: 0 0 30%; /* Filter bar takes up 30% of the page width */
margin-right: 20px;
border: 1px solid #ddd;
padding: 20px;
background-color: #f8f9fa;
border-radius: 5px;
height: max-content;
}
.data-display {
flex: 1; /* Data list takes up the remaining space */
}
.data-card {
border: 1px solid #ddd;
padding: 20px;
margin-bottom: 20px;
border-radius: 5px;
background-color: #fff;
}
.data-card h4 {
color: #6495ED;
}
.data-card p {
margin: 0;
padding: 10px 0;
}
.btn-primary {
/*border-color: #6495ED;*/
/*border-width: 10px;*/
/*background-color: #900D13;*/
color: #0047AB;
border: none;
}
.pagination {
display: flex;
justify-content: center; /* Center the pagination */
margin-top: 20px;
}
.pagination li {
display: inline-block;
padding: 10px
}
.page-item.active .page-link {
background-color: #900D13;
border-color: #900D13;
}
</style>
<?php
// Get the country code from the URL, e.g., bgd for Bangladesh
$country = isset($_GET['country']) ? pg_escape_string($con, $_GET['country']) : '';
// Get filter values from URL parameters
$adm1 = isset($_GET['adm1']) ? pg_escape_string($con, $_GET['adm1']) : '';
$adm2 = isset($_GET['adm2']) ? pg_escape_string($con, $_GET['adm2']) : '';
$adm3 = isset($_GET['adm3']) ? pg_escape_string($con, $_GET['adm3']) : '';
// Get the current page number from URL, default to page 1
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10; // Number of results per page
$offset = ($page - 1) * $perPage;
// If no country is provided, display an error
if (empty($country)) {
echo "<p>Error: No country specified.</p>";
exit;
}
// Dynamically determine the table name based on the country code
$table_name = strtolower($country); // Assuming table names are lowercase ISO codes
// Check if the table for the country exists
$check_table_query = "SELECT to_regclass('public.$table_name')";
$table_exists = pg_fetch_result(pg_query($con, $check_table_query), 0, 0);
if (!$table_exists) {
echo "<p>Error: No data available for the selected country.</p>";
exit;
}
// Query to get unique non-empty values for adm1, adm2, and adm3, alphabetically sorted
$unique_adm1_query = "SELECT DISTINCT adm1 FROM $table_name WHERE adm1 IS NOT NULL AND TRIM(adm1) != '' ORDER BY adm1 ASC";
$unique_adm2_query = "SELECT DISTINCT adm2 FROM $table_name WHERE adm2 IS NOT NULL AND TRIM(adm2) != '' ORDER BY adm2 ASC";
$unique_adm3_query = "SELECT DISTINCT adm3 FROM $table_name WHERE adm3 IS NOT NULL AND TRIM(adm3) != '' ORDER BY adm3 ASC";
$adm1_options = pg_query($con, $unique_adm1_query);
$adm2_options = pg_query($con, $unique_adm2_query);
$adm3_options = pg_query($con, $unique_adm3_query);
// Construct the base query for filters
$query = "SELECT * FROM $table_name WHERE 1=1";
// Add filters to the query if they are set
if (!empty($adm1)) {
$query .= " AND adm1 = '$adm1'";
}
if (!empty($adm2)) {
$query .= " AND adm2 = '$adm2'";
}
if (!empty($adm3)) {
$query .= " AND adm3 = '$adm3'";
}
// Query to get the total number of records (for pagination)
$totalQuery = pg_query($con, str_replace("*", "COUNT(*) as total", $query));
$totalResults = pg_fetch_assoc($totalQuery)['total'];
$totalPages = ceil($totalResults / $perPage);
// Sort the results alphabetically by `school_name`
$query .= " ORDER BY school_name ASC";
// Add pagination (LIMIT and OFFSET) to the query
$query .= " LIMIT $perPage OFFSET $offset";
// Execute the paginated query
$result = pg_query($con, $query);
// If the query fails, display an error
if (!$result) {
echo "<p>Error: Failed to retrieve data for $country.</p>";
exit;
}
?>
<body>
<link rel="stylesheet" href="./style.css" />
<link href="./index.css" rel="stylesheet" />
<?php include 'includes/header.php' ?>
<div class="page-layout">
<!-- Filter Bar -->
<div class="filter-bar">
<h4>Filter Schools</h4>
<br>
<form action="explore.php" method="get">
<!-- Keep the country in the URL -->
<input type="hidden" name="country" value="<?php echo htmlspecialchars($country); ?>">
<!-- adm1 Filter -->
<div class="form-group">
<label for="adm1">Admin Level 1</label><br>
<select name="adm1" id="adm1" class="form-control">
<option value="">All</option> <!-- Default "All" option at the top -->
<?php while ($row = pg_fetch_assoc($adm1_options)) { ?>
<option value="<?php echo htmlspecialchars($row['adm1']); ?>" <?php if ($adm1 == $row['adm1']) echo 'selected'; ?>>
<?php echo htmlspecialchars($row['adm1']); ?>
</option>
<?php } ?>
</select>
</div>
<br>
<!-- adm2 Filter -->
<div class="form-group">
<label for="adm2">Admin Level 2</label><br>
<select name="adm2" id="adm2" class="form-control">
<option value="">All</option> <!-- Default "All" option at the top -->
<?php while ($row = pg_fetch_assoc($adm2_options)) { ?>
<option value="<?php echo htmlspecialchars($row['adm2']); ?>" <?php if ($adm2 == $row['adm2']) echo 'selected'; ?>>
<?php echo htmlspecialchars($row['adm2']); ?>
</option>
<?php } ?>
</select>
</div>
<br>
<!-- adm3 Filter -->
<div class="form-group">
<label for="adm3">Admin Level 3</label><br>
<select name="adm3" id="adm3" class="form-control">
<option value="">All</option> <!-- Default "All" option at the top -->
<?php while ($row = pg_fetch_assoc($adm3_options)) { ?>
<option value="<?php echo htmlspecialchars($row['adm3']); ?>" <?php if ($adm3 == $row['adm3']) echo 'selected'; ?>>
<?php echo htmlspecialchars($row['adm3']); ?>
</option>
<?php } ?>
</select>
</div>
<br>
<!-- Filter Button -->
<button type="submit" class="btn btn-primary">Apply Filters</button>
</form>
</div>
<!-- Data Display -->
<div class="data-display">
<div class="row">
<?php while ($row = pg_fetch_assoc($result)) { ?>
<div class="col-md-6 col-lg-4">
<div class="data-card">
<h4><?php echo htmlspecialchars($row['school_name']); ?></h4>
<div class="row">
<div class="col-12">
<p><strong>Admin 1:</strong> <?php echo htmlspecialchars($row['adm1']); ?> |
<strong>Admin 2:</strong> <?php echo htmlspecialchars($row['adm2']); ?> |
<strong>Admin 3:</strong> <?php echo htmlspecialchars($row['adm3']); ?></p>
</div>
</div>
<!-- $tableData .= "<td onclick='redirectToSchool(\"" . htmlspecialchars($row['geo_id']) . "\")'>" . htmlspecialchars($row['geo_id']) . "</td>";-->
<!-- onclick = redirectToSchool(\"" . htmlspecialchars($row['geo_id']) . "\")'-->
<a href="#" onclick="redirectToSchool('<?php echo htmlspecialchars($row['geo_id']); ?>', '<?php echo htmlspecialchars($country); ?>')" class="btn btn-primary">View School Profile</a>
<!-- <a href="school_profile.php?id=--><?php //echo htmlspecialchars($row['id']); ?><!--" class="btn btn-primary">View School Profile</a>-->
</div>
</div>
<?php } ?>
</div>
<!-- Pagination Section -->
<nav aria-label="Page navigation">
<ul class="pagination">
<?php
// Determine the range of pages to show
$maxVisiblePages = 8; // Maximum number of pages to show
$startPage = max(1, $page - floor($maxVisiblePages / 2));
$endPage = min($totalPages, $startPage + $maxVisiblePages - 1);
// Adjust startPage if we're close to the end of the page range
if ($endPage - $startPage + 1 < $maxVisiblePages) {
$startPage = max(1, $endPage - $maxVisiblePages + 1);
}
// Show previous arrow
if ($page > 1) { ?>
<li class="page-item">
<a class="page-link" href="?country=<?php echo $country; ?>&page=<?php echo $page - 1; ?>&adm1=<?php echo $adm1; ?>&adm2=<?php echo $adm2; ?>&adm3=<?php echo $adm3; ?>" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<?php } ?>
<!-- Display page links in the defined range -->
<?php for ($i = $startPage; $i <= $endPage; $i++) { ?>
<li class="page-item <?php if ($i == $page) echo 'active'; ?>">
<a class="page-link" href="?country=<?php echo $country; ?>&page=<?php echo $i; ?>&adm1=<?php echo $adm1; ?>&adm2=<?php echo $adm2; ?>&adm3=<?php echo $adm3; ?>"><?php echo $i; ?></a>
</li>
<?php } ?>
<!-- Show next arrow -->
<?php if ($page < $totalPages) { ?>
<li class="page-item">
<a class="page-link" href="?country=<?php echo $country; ?>&page=<?php echo $page + 1; ?>&adm1=<?php echo $adm1; ?>&adm2=<?php echo $adm2; ?>&adm3=<?php echo $adm3; ?>" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
</div>
</div>
</body>
<?php
// Include your footer file if needed
include('footer.php');
?>