-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpost.php
73 lines (63 loc) · 2.26 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
<?php
include('db.php');
class Post
{
private $db;
public function __construct($db){
$this->db = $db;
}
public function addPost($title,$description,$image,$date,$slug){
$sql = "INSERT INTO posts(title,description,image,created_at,slug)VALUES('$title','$description','$image','$date','$slug')";
$result = mysqli_query($this->db,$sql);
if($result){
if($_POST['tags']){
$tags = $_POST['tags'];
$lastInsertedId = mysqli_insert_id($this->db);
foreach($tags as $tag){
$sql="INSERT INTO post_tags(post_id,tag_id)VALUES('$lastInsertedId',$tag)";
$result = mysqli_query($this->db,$sql);
}
}
}
return $result;
}
public function getPost(){
if(isset($_GET['tag'])){
$tag = $_GET['tag'];
$sql = "SELECT *
FROM posts
INNER JOIN post_tags ON posts.id = post_tags.post_id
INNER JOIN tags ON tags.id = post_tags.tag_id
WHERE tags.tag='$tag'";
$result = mysqli_query($this->db,$sql);
return $result;
}
$sql = "SELECT * from posts";
$result = mysqli_query($this->db, $sql);
return $result;
}
public function updatePost($title,$description,$slug){
$newImage = $_FILES['image']['name'];
if(!empty($newImage)){
$image = uploadImage();
$sql = "UPDATE posts SET title ='$title',description='$description',image = '$image' WHERE slug = '$slug'";
$result = mysqli_query($this->db,$sql);
return $result;
}else{
$sql = "UPDATE posts SET title ='$title',description='$description' WHERE slug = '$slug'";
$result = mysqli_query($this->db,$sql);
return $result;
}
}
public function deletePostBySlug($slug){
$sql = "DELETE FROM posts WHERE slug='$slug'";
$result = mysqli_query($this->db,$sql);
return $result;
}
public function getSinglePost($slug){
$sql = "SELECT * FROM posts WHERE slug='$slug'";
$result = mysqli_query($this->db, $sql);
return $result;
}
}
?>