-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPark.php
More file actions
186 lines (117 loc) · 5.08 KB
/
Park.php
File metadata and controls
186 lines (117 loc) · 5.08 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
// __DIR__ is a *magic constant* with the directory path containing this file.
// This allows us to correctly require_once Model.php, no matter where this file is being required from.
require_once __DIR__ . '/Model.php';
class Park extends Model
{
/** Insert a new entry into the database */
protected function insert()
{
// @TODO: Use prepared statements to ensure data security
if (isset($this->attributes['name'])) {
$name = $this->attributes['name'];
}
if (isset($this->attributes['location'])) {
$email = $this->attributes['location'];
}
if (isset($this->attributes['date_established'])) {
$password = $this->attributes['date_established'];
}
if (isset($this->attributes['area_in_acres'])) {
$password = $this->attributes['area_in_acres'];
}
if (isset($this->attributes['description'])) {
$password = $this->attributes['description'];
}
$stmt = self::$dbc->prepare('INSERT INTO national_parks (name, location, date_established, area_in_acres, description) VALUES (:name, :location, :date_established, :area_in_acres, :description)');
// @TODO: You will need to iterate through all the attributes to build the prepared query
foreach ($this->attributes as $key=>$value) {
$stmt->bindValue(':' . $key, $value, PDO::PARAM_STR);
}
// @TODO: After the insert, add the id back to the attributes array
// so the object properly represents a DB record
$stmt->execute();
$lastId=self::$dbc->lastInsertId();
$this->attributes['id'] = $lastId;
}
/** Update existing entry in the database */
protected function update()
{
// @TODO: Use prepared statements to ensure data security
if (isset($this->attributes['name'])) {
$name = $this->attributes['name'];
}
if (isset($this->attributes['location'])) {
$email = $this->attributes['location'];
}
if (isset($this->attributes['date_established'])) {
$password = $this->attributes['date_established'];
}
if (isset($this->attributes['area_in_acres'])) {
$password = $this->attributes['area_in_acres'];
}
if (isset($this->attributes['description'])) {
$password = $this->attributes['description'];
}
$stmt = self::$dbc->prepare('UPDATE national_parks SET name = :name, location = :location, date_established = :date_established, area_in_acres = :area_in_acres, description = :description WHERE id=:id');
// @TODO: You will need to iterate through all the attributes to build the prepared query
foreach ($this->attributes as $key=>$value) {
$stmt->bindValue(':' . $key, $value, PDO::PARAM_STR);
}
$stmt->bindValue(':' . 'id', $this->attributes['id'], PDO::PARAM_INT);
$stmt->execute();
}
/**
* Find a single record in the DB based on its id
*
* @param int $id id of the user entry in the database
*
* @return User An instance of the User class with attributes array set to values from the database
*/
public static function find($id)
{
// Get connection to the database
self::dbConnect();
// @TODO: Create select statement using prepared statements
$selectQuery = 'SELECT * FROM national_parks WHERE id=:id';
$stmt = self::$dbc->prepare($selectQuery);
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
// @TODO: Store the result in a variable named $result
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// The following code will set the attributes on the calling object based on the result variable's contents
$instance = null;
if ($result) {
$instance = new static($result);
}
return $instance;
}
/**
* Find all records in a table
*
* @return User[] Array of instances of the User class with attributes set to values from database
*/
public static function all($offset,$limit)
{
self::dbConnect();
// @TODO: Learning from the find method, return all the matching records
$query = "SELECT * FROM national_parks LIMIT :limit OFFSET :offset";
$stmt = self::$dbc->prepare($query);
var_dump($limit);
$stmt->bindValue(":limit", $limit, PDO::PARAM_INT);
$stmt->bindValue(":offset", $offset, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public static function delete($id)
{
// Get connection to the database
self::dbConnect();
// @TODO: Create select statement using prepared statements
$selectQuery = 'DELETE * FROM national_parks WHERE id=:id';
$stmt = self::$dbc->prepare($selectQuery);
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
}
}