-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
154 lines (122 loc) · 4.99 KB
/
admin.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
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
<?php
include_once 'db.php';
class Articles extends DB {
function getArticles() {
$sql = 'select * from articles where published = 1';
$sth = $this->dbh->prepare($sql);
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
function getArticle($id) {
$params = [':id' => $id];
$sql = 'select * from articles where id = :id and published = 1';
$sth = $this->dbh->prepare($sql);
$sth->execute($params);
return $sth->fetch(PDO::FETCH_ASSOC);
}
function deleteArticle($id) {
$params = [':id' => $id];
$sql = 'UPDATE articles SET published = 0 WHERE id = :id';
$sth = $this->dbh->prepare($sql);
$sth->execute($params);
return $sth->rowCount();
}
function addArticle($article) {
$params = [':title' => $article["title"], ':content' => $article["content"]];
$sql = 'INSERT INTO articles(title, content, published, image) VALUES(:title, :content, 1, :title)';
$sth = $this->dbh->prepare($sql);
$result = $sth->execute($params);
return $sth->rowCount();
}
function updateArticle($article) {
$params = [':id' => $article['id'], ':title' => $article["title"], ':content' => $article["content"]];
$sql = 'UPDATE articles SET title = :title, content = :content WHERE id = :id';
$sth = $this->dbh->prepare($sql);
$sth->execute($params);
return $sth->rowCount();
}
function getArticlesJSON() {
$articles = $this->getArticles();
echo json_encode($articles);
}
function getArticlesHTML() {
$articles = $this->getArticles();
$result = '<table border="1">';
for($i=0; $i < count($articles); $i++) {
$result .= '<tr>
<td>'. $articles[$i]["title"]. '</td>
<td>' .$articles[$i]["content"] . '</td>
<td><a href="admin.php?delete&id=' . $articles[$i]["id"].'">Delete</a></td>
<td><a href="admin.php?edit&id='. $articles[$i]["id"] . '">Edit</a></td>
</tr>';
}
$result .= '</table>';
echo $result;
}
}
$articlesClass = new Articles();
$article = '';
$edit = false;
if (isset($_GET['articlesToHTML'])) {
return $articlesClass->getArticlesHTML();
die;
}
if (isset($_GET['articlesToJSON'])) {
return $articlesClass->getArticlesJSON();
die;
}
if (isset($_GET["delete"]) && !empty($_GET["id"])) {
$result = $articlesClass->deleteArticle($_GET["id"]);
if ($result === 1) {
echo "Article Deleted";
}
}
if (isset($_GET["edit"]) && !empty($_GET["id"])) {
$edit = true;
$article = $articlesClass->getArticle($_GET["id"]);
}
if (!empty($_POST["title"]) && empty($_POST["id"]) ) {
// $_FILES
$result = $articlesClass->addArticle($_POST);
}
if (isset($_GET["update"]) && !empty($_POST["title"]) && !empty($_POST["id"])) {
$result = $articlesClass->updateArticle($_POST);
}
$articles = $articlesClass->getArticles();
if ($articles === false) {
echo "Empty List";
}
else {
}
?>
<?php
include ('header.php');
?>
<!--MAIN-->
<div class="admin-main">
<table class="table">
<tr>
<td>Title</td>
<td>Content</td>
<td></td>
<td></td>
</tr>
<?php for($i=0; $i < count($articles); $i++) { ?>
<tr>
<td><?php echo $articles[$i]["title"];?></td>
<td><?php echo $articles[$i]["content"];?></td>
<td><a href="admin.php?delete&id=<?php echo $articles[$i]["id"];?>">Delete</a></td>
<td><a href="admin.php?edit&id=<?php echo $articles[$i]["id"];?>">Edit</a></td>
</tr>
<?php }?>
</table>
<form enctype="multipart/form-data" method="POST" action="admin.php?<?php echo $edit ? 'update' : '' ?>">
<h3>Add article</h3>
<input class="form-control" type="hidden" name="id" value="<?php echo isset($article["id"]) ? $article["id"] : ""; ?>"/>
<input class="form-control" type="text" name="title" placeholder="Title" value="<?php echo isset($article["title"]) ? $article["title"] : ""; ?>"/>
<textarea class="form-control" type="text" name="content" placeholder="Content" value="<?php echo isset($article["content"]) ? $article["content"] : ""; ?>"/></textarea>
<input name="file" type="file" />
<input class="btn btn-primary" type="submit" value="Submit"/>
</form>
<script src="js/upload.js"></script>
</div>