|
|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
<?php
|
|
|
|
|
include_once __DIR__ . '/person.php';
|
|
|
|
|
$dbconn = pg_connect("host=localhost dbname=contacts2");
|
|
|
|
|
|
|
|
|
|
class Location {
|
|
|
|
|
@ -11,6 +12,7 @@ class Location {
|
|
|
|
|
$this->street = $street;
|
|
|
|
|
$this->city = $city;
|
|
|
|
|
$this->state = $state;
|
|
|
|
|
$this->inhabitants = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class Locations {
|
|
|
|
|
@ -27,9 +29,9 @@ class Locations {
|
|
|
|
|
|
|
|
|
|
return self::all();
|
|
|
|
|
}
|
|
|
|
|
static function update($updatedLocation){
|
|
|
|
|
static function update($updated_location){
|
|
|
|
|
$query = "UPDATE locations SET street = $1, city = $2, state = $3 WHERE id = $4";
|
|
|
|
|
$query_params = array($updatedLocation->street, $updatedLocation->city, $updatedLocation->state, $updatedLocation->id);
|
|
|
|
|
$query_params = array($updated_location->street, $updated_location->city, $updated_location->state, $updated_location->id);
|
|
|
|
|
$result = pg_query_params($query, $query_params);
|
|
|
|
|
|
|
|
|
|
return self::all();
|
|
|
|
|
@ -37,18 +39,43 @@ class Locations {
|
|
|
|
|
static function all(){
|
|
|
|
|
$locations = array();
|
|
|
|
|
|
|
|
|
|
$results = pg_query("SELECT * FROM locations");
|
|
|
|
|
$results = pg_query("SELECT
|
|
|
|
|
locations.*,
|
|
|
|
|
people.id AS person_id,
|
|
|
|
|
people.name,
|
|
|
|
|
people.age
|
|
|
|
|
FROM locations
|
|
|
|
|
LEFT JOIN people
|
|
|
|
|
ON locations.id = people.home_id
|
|
|
|
|
ORDER BY locations.id ASC");
|
|
|
|
|
|
|
|
|
|
$row_object = pg_fetch_object($results);
|
|
|
|
|
$last_location_id = null;
|
|
|
|
|
while($row_object){
|
|
|
|
|
|
|
|
|
|
if($row_object->id !== $last_location_id){
|
|
|
|
|
$new_location = new Location(
|
|
|
|
|
intval($row_object->id),
|
|
|
|
|
$row_object->id,
|
|
|
|
|
$row_object->street,
|
|
|
|
|
$row_object->city,
|
|
|
|
|
$row_object->state
|
|
|
|
|
);
|
|
|
|
|
$locations[] = $new_location;
|
|
|
|
|
$last_location_id = $row_object->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($row_object->person_id){
|
|
|
|
|
$new_person = new Person(
|
|
|
|
|
intval($row_object->person_id),
|
|
|
|
|
$row_object->name,
|
|
|
|
|
$row_object->age
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$locations_length = count($locations);
|
|
|
|
|
$last_index_of_locations = $locations_length-1;
|
|
|
|
|
$most_recently_added_location = $locations[$last_index_of_locations];
|
|
|
|
|
$most_recently_added_location->inhabitants[] = $new_person;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row_object = pg_fetch_object($results);
|
|
|
|
|
}
|
|
|
|
|
|