-
Notifications
You must be signed in to change notification settings - Fork 0
/
Post.php
85 lines (74 loc) · 1.79 KB
/
Post.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
<?php
/**
*
*/
class Post
{
private $db;
public function __construct()
{
$this->db = new Database;
}
public function getPosts(){
$this->db->query('SELECT *,
posts.id as postId,
users.id as userId,
posts.created_at as postCreated,
users.created_at as userCreated
FROM posts
INNER JOIN users
ON posts.user_id = users.id
WHERE posts.status = 1
ORDER BY posts.created_at DESC
');
$results = $this->db->resultSet();
return $results;
}
public function addPost($data){
//Creating query
$this->db->query('INSERT INTO posts (title, user_id, body) VALUES (:title, :user_id, :body)');
//Binding values
$this->db->bind(':title', $data['title']);
$this->db->bind(':user_id', $data['user_id']);
$this->db->bind(':body', $data['body']);
//Execute
if($this->db->execute()){
return true;
} else{
return false;
}
}
public function getPostById($id){
$this->db->query('SELECT * FROM posts WHERE id = :id');
$this->db->bind(':id', $id);
$row = $this->db->single();
return $row;
}
public function updatePost($data){
//Creating query
$this->db->query('UPDATE posts SET title = :title, body = :body WHERE id = :id');
//Binding values
$this->db->bind(':id', $data['id']);
$this->db->bind(':title', $data['title']);
$this->db->bind(':body', $data['body']);
//Execute
if($this->db->execute()){
return true;
} else{
return false;
}
}
public function deletePost($id){
//Creating query
$this->db->query('UPDATE posts SET status = 0 WHERE id = :id');
//Binding values
$this->db->bind(':id', $id);
//Execute
if($this->db->execute()){
return true;
} else{
return false;
}
}
}
?>