-
Notifications
You must be signed in to change notification settings - Fork 0
/
restaurant.class.php
87 lines (73 loc) · 2.54 KB
/
restaurant.class.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
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
<?php
declare(strict_types = 1);
class Restaurant {
public int $id;
public string $name;
public string $address;
public string $category;
public string $image;
public function __construct(int $id, string $name, string $address, string $category, string $image)
{
$this->id = $id;
$this->name = $name;
$this->address = $address;
$this->category = $category;
$this->image = $image;
}
static function countRestaurant(PDO $db){
$stmt = $db->prepare('
SELECT MAX(RestaurantId)
FROM Restaurant
');
$stmt->execute(array());
$max = $stmt->fetch();
return $max['MAX(RestaurantId)'];
}
static function searchRestaurants(PDO $db, string $search, int $count) : array {
$stmt = $db->prepare('SELECT RestaurantId, RestaurantName, RestaurantAddress, Category, RestaurantPhoto FROM Restaurant WHERE RestaurantName LIKE ? LIMIT ?');
$stmt->execute(array($search . '%', $count));
$restaurants = array();
while ($restaurant = $stmt->fetch()) {
$restaurants[] = new Restaurant(
intval($restaurant['RestaurantId']),
$restaurant['RestaurantName'],
$restaurant['RestaurantAddress'],
$restaurant['Category'],
$restaurant['RestaurantPhoto']
);
}
return $restaurants;
}
static function getRestaurants(PDO $db, int $count) : array {
$stmt = $db->prepare('SELECT RestaurantId, RestaurantName, RestaurantAddress, Category, RestaurantPhoto FROM Restaurant LIMIT ?');
$stmt->execute(array($count));
$Restaurants = array();
while ($Restaurant = $stmt->fetch()) {
$Restaurants[] = new Restaurant(
intval($Restaurant['RestaurantId']),
$Restaurant['RestaurantName'],
$Restaurant['RestaurantAddress'],
$Restaurant['Category'],
$Restaurant['RestaurantPhoto']
);
}
return $Restaurants;
}
static function getRestaurant(PDO $db, int $id) : Restaurant {
$stmt = $db->prepare('
SELECT RestaurantId, RestaurantName, RestaurantAddress, Category, RestaurantPhoto
FROM Restaurant
WHERE RestaurantId = ?
');
$stmt->execute(array($id));
$Restaurant = $stmt->fetch();
return new Restaurant(
intval($Restaurant['RestaurantId']),
$Restaurant['RestaurantName'],
$Restaurant['RestaurantAddress'],
$Restaurant['Category'],
$Restaurant['RestaurantPhoto']
);
}
}
?>