Last active
February 15, 2018 13:47
-
-
Save ElRochito/61bed5cfc3ec577b086299ab144089d4 to your computer and use it in GitHub Desktop.
City search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Test PHP</title> | |
</head> | |
<body> | |
<form action = "json.php" method="post"> | |
<label for="cityform">Veuillez choisir un nom de ville : </label> | |
<input type="text" id="cityform" name="name" | |
placeholder="Saisissez la ville" | |
size="30"> | |
<input type = "submit" value = "Envoyer"> | |
</form> | |
<?php | |
if (isset($_POST["name"]) && !empty($_POST["name"])) { | |
ini_set('memory_limit', '500M'); | |
$url = 'city.list.json'; // path to your JSON file | |
$data = file_get_contents($url); // put the contents of the file into a variable | |
$json = json_decode($data); // decode the JSON feed | |
// la méthode "extract" permet d'extraire tout les clés d'un tableau et à en faire une varialle | |
// dans notre cas `$_POST["name"]` sera accessible via `$name` | |
extract($_POST); | |
?><h2>Ville recherchée : <?= $name; ?></h2><?php | |
$city_match = (object) array( | |
'name' => '', | |
'country' => '' | |
); | |
foreach ($json as $city) { | |
if (strtolower($name) == strtolower($city->name)){ | |
$city_match = $city; | |
break; | |
} | |
} | |
if($city_match){ | |
?> | |
<ul> | |
<li><?= $city_match->name ?> (<?= $city_match->country ?>)</li> | |
</ul> | |
<?php | |
} else { | |
echo 'La ville saisie nexiste pas'; | |
} | |
} else { | |
echo 'Veuillez saisir une ville.'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment