-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle.php
More file actions
179 lines (159 loc) · 6.04 KB
/
Copy patharticle.php
File metadata and controls
179 lines (159 loc) · 6.04 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
<div class="container">
<!-- Button trigger modal -->
<button type="button" class="btn btn-secondary mb-2" data-bs-toggle="modal" data-bs-target="#modalTambah">
<i class="bi bi-plus-lg"></i> Tambah Article
</button>
<div class="row">
<div class="table-responsive" id="article_data">
</div>
<!-- Awal Modal Tambah-->
<div class="modal fade" id="modalTambah" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="staticBackdropLabel">Tambah Article</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post" action="" enctype="multipart/form-data">
<div class="modal-body">
<div class="mb-3">
<label for="formGroupExampleInput" class="form-label">Judul</label>
<input type="text" class="form-control" name="judul" placeholder="Tuliskan Judul Artikel" required>
</div>
<div class="mb-3">
<label for="floatingTextarea2">Isi</label>
<textarea class="form-control" placeholder="Tuliskan Isi Artikel" name="isi" required></textarea>
</div>
<div class="mb-3">
<label for="formGroupExampleInput2" class="form-label">Gambar</label>
<input type="file" class="form-control" name="gambar">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<input type="submit" value="simpan" name="simpan" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
<!-- Akhir Modal Tambah-->
</div>
</div>
<script>
$(document).ready(function(){
load_data();
function load_data(hlm){
$.ajax({
url : "article_data.php",
method : "POST",
data : {
hlm: hlm
},
success : function(data){
$('#article_data').html(data);
}
})
}
$(document).on('click', '.halaman', function(){
var hlm = $(this).attr("id");
load_data(hlm);
});
});
</script>
<?php
include "upload_foto.php";
//jika tombol simpan diklik
if (isset($_POST['simpan'])) {
$judul = $_POST['judul'];
$isi = $_POST['isi'];
$tanggal = date("Y-m-d H:i:s");
$username = $_SESSION['username'];
$gambar = '';
$nama_gambar = $_FILES['gambar']['name'];
//jika ada file yang dikirim
if ($nama_gambar != '') {
//panggil function upload_foto untuk cek spesifikasi file yg dikirimkan user
//function ini memiliki 2 keluaran yaitu status dan message
$cek_upload = upload_foto($_FILES["gambar"]);
//cek status true/false
if ($cek_upload['status']) {
//jika true maka message berisi nama file gambar
$gambar = $cek_upload['message'];
} else {
//jika true maka message berisi pesan error, tampilkan dalam alert
echo "<script>
alert('" . $cek_upload['message'] . "');
document.location='admin.php?page=article';
</script>";
die;
}
}
//cek apakah ada id yang dikirimkan dari form
if (isset($_POST['id'])) {
//jika ada id, lakukan update data dengan id tersebut
$id = $_POST['id'];
if ($nama_gambar == '') {
//jika tidak ganti gambar
$gambar = $_POST['gambar_lama'];
} else {
//jika ganti gambar, hapus gambar lama
unlink("img/" . $_POST['gambar_lama']);
}
$stmt = $conn->prepare("UPDATE article
SET
judul =?,
isi =?,
gambar = ?,
tanggal = ?,
username = ?
WHERE id = ?");
$stmt->bind_param("sssssi", $judul, $isi, $gambar, $tanggal, $username, $id);
$simpan = $stmt->execute();
} else {
//jika tidak ada id, lakukan insert data baru
$stmt = $conn->prepare("INSERT INTO article (judul,isi,gambar,tanggal,username)
VALUES (?,?,?,?,?)");
$stmt->bind_param("sssss", $judul, $isi, $gambar, $tanggal, $username);
$simpan = $stmt->execute();
}
if ($simpan) {
echo "<script>
alert('Simpan data sukses');
document.location='admin.php?page=article';
</script>";
} else {
echo "<script>
alert('Simpan data gagal');
document.location='admin.php?page=article';
</script>";
}
$stmt->close();
$conn->close();
}
//jika tombol hapus diklik
if (isset($_POST['hapus'])) {
$id = $_POST['id'];
$gambar = $_POST['gambar'];
if ($gambar != '') {
//hapus file gambar
unlink("img/" . $gambar);
}
$stmt = $conn->prepare("DELETE FROM article WHERE id =?");
$stmt->bind_param("i", $id);
$hapus = $stmt->execute();
if ($hapus) {
echo "<script>
alert('Hapus data sukses');
document.location='admin.php?page=article';
</script>";
} else {
echo "<script>
alert('Hapus data gagal');
document.location='admin.php?page=article';
</script>";
}
$stmt->close();
$conn->close();
}
?>