-
Notifications
You must be signed in to change notification settings - Fork 1
/
countries.php
28 lines (25 loc) · 1.07 KB
/
countries.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
<?php
$country_data = file_get_contents('https://restcountries.com/v3.1/all');
$country_data = json_decode($country_data, true);
usort($country_data, function($a, $b) {
return strcmp($a['name']['common'], $b['name']['common']);
});
$selected_country = isset($_GET['country']) ? $_GET['country'] : 'in';
echo '<form class=button action="index.php" method="GET">';
echo '<label for="country">Select a country:</label>';
echo '<select name="country" id="country">';
foreach ($country_data as $country) {
$country_data = $country['name']['common'];
$iso_code = strtolower($country['cca2']);
$selected = ($selected_country == $iso_code) ? 'selected' : '';
echo "<option value=\"$iso_code\" $selected>$country_data</option>";
}
echo '</select>';
echo '<button class=button type="submit">Submit</button>';
echo '</form>';
// Add the button to load the map
echo '<form class=button action="sites.php" method="GET">';
echo '<input type="hidden" name="country" value="' . $selected_country . '">';
echo '<button class=button type="submit">List</button>';
echo '</form>';
?>