|
2 | 2 | class Country {
|
3 | 3 | private $db;
|
4 | 4 |
|
5 |
| - public function __construct() { |
| 5 | + public function __construct() |
| 6 | + { |
6 | 7 | $this->db = new DatabaseController;
|
7 | 8 | }
|
8 | 9 |
|
9 |
| - public function getCountries() { |
| 10 | + public function getContinents() |
| 11 | + { |
| 12 | + // https://stackoverflow.com/a/2350145 |
| 13 | + $this->db->query("SELECT SUBSTRING(COLUMN_TYPE,5) as :name |
| 14 | + FROM information_schema.COLUMNS |
| 15 | + WHERE TABLE_SCHEMA = :database |
| 16 | + AND TABLE_NAME = :table |
| 17 | + AND COLUMN_NAME = :column"); |
| 18 | + |
| 19 | + $this->db |
| 20 | + ->bind(":name", "continents") |
| 21 | + ->bind(":database", DB_NAME) |
| 22 | + ->bind(":table", "countries") |
| 23 | + ->bind(":column", "continent"); |
| 24 | + |
| 25 | + $results = []; |
| 26 | + |
| 27 | + preg_match_all( |
| 28 | + "/'([^']+?)',{0,1}/", |
| 29 | + $this->db->execute()->fetchAll()[0]["continents"], |
| 30 | + $results |
| 31 | + ); |
| 32 | + |
| 33 | + return $results[1]; |
| 34 | + } |
| 35 | + |
| 36 | + public function getCountries() |
| 37 | + { |
10 | 38 | $this->db->query("SELECT * FROM `countries`");
|
11 | 39 |
|
12 |
| - $result = $this->db->execute()->fetchAll(); |
| 40 | + return $this->db->execute()->fetchAll(); |
| 41 | + } |
| 42 | + |
| 43 | + public function getCountry(int $id) |
| 44 | + { |
| 45 | + $this->db->query("SELECT * FROM `countries` WHERE `id` = :id"); |
| 46 | + |
| 47 | + $this->db->bind(":id", $id); |
| 48 | + |
| 49 | + return $this->db->execute()->fetch(); |
| 50 | + } |
| 51 | + |
| 52 | + public function deleteCountry(int $id) |
| 53 | + { |
| 54 | + $this->db->query("DELETE FROM `countries` WHERE `id` = :id"); |
| 55 | + |
| 56 | + $this->db->bind(":id", $id); |
| 57 | + |
| 58 | + $this->db->execute(); |
| 59 | + |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + public function editCountry(int $id, string $name, string $capitalCity, string $continent, int $population) |
| 64 | + { |
| 65 | + $this->db->query("UPDATE `countries` |
| 66 | + SET |
| 67 | + `name` = :name, |
| 68 | + `capitalCity` = :capitalCity, |
| 69 | + `continent` = :continent, |
| 70 | + `population` = :population |
| 71 | + WHERE `id` = :id"); |
| 72 | + |
| 73 | + $this->db |
| 74 | + ->bind(":name", $name) |
| 75 | + ->bind(":capitalCity", $capitalCity) |
| 76 | + ->bind(":continent", $continent) |
| 77 | + ->bind(":population", $population) |
| 78 | + ->bind(":id", $id); |
| 79 | + |
| 80 | + $this->db->execute(); |
13 | 81 |
|
14 |
| - return $result; |
| 82 | + return $this; |
15 | 83 | }
|
16 | 84 | }
|
0 commit comments