-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.php
54 lines (47 loc) · 1.55 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
<?php
$city = filter_input(INPUT_GET, "city", FILTER_SANITIZE_STRING);
include_once "config/Database.php";
if ($city) {
$query = 'SELECT * FROM city WHERE Name LIKE :city ORDER BY Population DESC';
$statement = $db->prepare($query);
$statement->bindValue(':city', "%" . $city . "%");
$statement->execute();
//$statement->debugDumpParams();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$statement->closeCursor();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDO Tutorial</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<h1>Search Cities</h1>
</header>
<section>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="GET">
<h2 id="enter-a-city">Enter a City:</h2>
<input type="text" id="city" name="city" aria-labelledby="enter-a-city" autofocus required>
<button>Submit</button>
</form>
</section>
<section>
<?php
if (!empty($results)) {
echo "<h2>" . count($results) . " Results</h2>";
foreach ($results as $result) {
echo "<p>{$result['Name']} - Pop: {$result['Population']}</p>";
}
} else {
echo "<p>No Results.</p>";
}
?>
</section>
</body>
</html>