-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
148 lines (112 loc) · 4.76 KB
/
index.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
<?php
// Fetch list of countries from API
$apiUrlforCountriesList = "https://restcountries.com/v3.1/all?fields=name";
$getListofCountries = file_get_contents($apiUrlforCountriesList);
$ListofCountries = json_decode($getListofCountries, true);
// generate a random number to pick a random country
$randomNumber = rand(0, 249); //there are 249 countries in the API
// extract the common name of a random country
$randomCountry = $ListofCountries[$randomNumber]['name']['common'];
//print the random country to console
echo "<script>console.log('Random Country: $randomCountry');</script>";
//$encodedCountryName = urlencode($randomCountry); didn't work!
//fetch data for the random country
$countryDataURL = "https://restcountries.com/v3.1/name/" . $randomCountry;
// Check if the URL contains a space
if (strpos($countryDataURL, ' ') !== false) {
// If it does, replace spaces with %20
$countryDataURL = str_replace(' ', '%20', $countryDataURL);
}
/*
testing if the URL contains a space
$pattern = '/\s/'; //pattern to match white spaces
$matchResult = preg_match($pattern, $countryDataURL);
echo "<script>";
if ($matchResult) {
echo "console.log('URL contains a space!');";
} else {
echo "console.log('URL does not contain a space.');";
}
echo "</script>";*/
$GetCountryData = file_get_contents($countryDataURL);
$countryData = json_decode($GetCountryData, true);
//var_dump($countryData);
//print the country data to console to check it's correct
echo "<script>console.log('Country Data: " . json_encode($countryData) . "');</script>";
$countryFlagImage = $countryData[0]["flags"]["svg"];
// if flag is missing Alt text, then display "Image of the flag of country"
$countryFlagAlt = $countryData[0]["flags"]["alt"] ?? "Flag of " . $randomCountry;
$region = $countryData[0]["region"];
$population = $countryData[0]["population"];
//$languages = $countryData[0]["languages"];
//var_dump($languages);
//loop through the languages array and extract the names of the languages
$languages = array();
foreach($countryData[0]["languages"] as $language){
$languages[] = $language; //add the language to the languages array
}
//change the languages array to a string
$languages = implode(", ", $languages); // implode(string $separator, array $array): string
$capital = $countryData[0]["capital"][0];
$currencies = array();
foreach($countryData[0]["currencies"] as $currency){
$currencies[] = $currency["name"]; //add the currency to the currencies array
}
$currency = implode(", ", $currencies);
/*to fix:
//☑️ fixed 1. when a country has spaces in the name, the fetch request fails, but https://restcountries.com/v3.1/name/Western%20Sahara is correct, for example.
// ☑️ Fixed 2. sometimes there is not any Alt text for a flag - apply a default Alt text using the country name variable
//it doesn't like https://restcountries.com/v3.1/name/Saint%20Barthélemy in borwser it's fine, but not in fetch request could be the accent
3. https://restcountries.com/v3.1/name/São%20Tomé%20and%20Príncipe - issue with fetching but fine in browser
4. issue with Bouvet Island:
Warning
: Undefined array key "capital" in
C:\xampp\htdocs\aaa-php-country-facts\index.php
on line
74
Warning
: Trying to access array offset on value of type null in
C:\xampp\htdocs\aaa-php-country-facts\index.php
on line
74
Warning
: Undefined array key "currencies" in
C:\xampp\htdocs\aaa-php-country-facts\index.php
on line
77
Warning
: foreach() argument must be of type array|object, null given in
C:\xampp\htdocs\aaa-php-country-facts\index.php
on line
77
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Country Fact Generator</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<h1>🌎 Country Fact Generator 🌎</h1>
</header>
<main>
<section>
<h2 id="countryName"><?= $randomCountry?></h2>
<img id="countryFlag" src="<?= $countryFlagImage;?>" alt="<?=$countryFlagAlt; ?>"/>
<ul>
<li>Region: <span id="countryRegion"><?= $region;?></span></li>
<li>Population: <span id="countryPopulation"><?= $population;?></span></li>
<li>Languages: <span id="countryLanguages"><?= $languages;?></span></li>
<li>Capital: <span id="countryCapital"><?= $capital;?></span></li>
<li>Currency: <span id="countryCurrency"><?= $currency;?></span></li>
</ul>
</section>
<button id="newCountryBtn">New country</button>
</main>
<script type="text/javascript" src="script.js"></script>
</body>
</html>