-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery.php
More file actions
186 lines (166 loc) · 6.35 KB
/
Copy pathgallery.php
File metadata and controls
186 lines (166 loc) · 6.35 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
179
180
181
182
183
184
185
<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 Gallery
</button>
<div class="row">
<div class="table-responsive" id="gallery_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 Gambar</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="formGroupExampleInput2" class="form-label">Gambar</label>
<input type="file" class="form-control" name="gambar" required>
</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 class="modal fade" id="imageModal" tabindex="-1" aria-labelledby="imageModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="imageModalLabel">Gambar Besar</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<img id="modalImage" src="" class="img-fluid" alt="Image">
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
load_data();
function load_data(hlm){
$.ajax({
url : "gallery_data.php",
method : "POST",
data : {
hlm: hlm
},
success : function(data){
$('#gallery_data').html(data);
}
})
}
$(document).on('click', '.halaman', function(){
var hlm = $(this).attr("id");
load_data(hlm);
});
$(document).on('click', '.image-thumbnail', function() {
var imageUrl = $(this).data('large'); // Ambil URL gambar besar dari atribut data-large
$('#modalImage').attr('src', imageUrl); // Set gambar besar pada modal
var imageModal = new bootstrap.Modal($('#imageModal')); // Inisialisasi modal
imageModal.show(); // Tampilkan modal
});
});
</script>
<?php
include "upload_foto.php";
//jika tombol simpan diklik
if (isset($_POST['simpan'])) {
$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=gallery';
</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 gallery
SET
username = ?,
gambar = ?,
tanggal = ?
WHERE id = ?");
$stmt->bind_param("sssi", $username, $gambar, $tanggal, $id);
$simpan = $stmt->execute();
} else {
//jika tidak ada id, lakukan insert data baru
$stmt = $conn->prepare("INSERT INTO gallery (username,gambar,tanggal)
VALUES (?,?,?)");
$stmt->bind_param("sss", $username, $gambar, $tanggal);
$simpan = $stmt->execute();
}
if ($simpan) {
echo "<script>
alert('Simpan data sukses');
document.location='admin.php?page=gallery';
</script>";
} else {
echo "<script>
alert('Simpan data gagal');
document.location='admin.php?page=gallery';
</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 gallery WHERE id =?");
$stmt->bind_param("i", $id);
$hapus = $stmt->execute();
if ($hapus) {
echo "<script>
alert('Hapus data sukses');
document.location='admin.php?page=gallery';
</script>";
} else {
echo "<script>
alert('Hapus data gagal');
document.location='admin.php?page=gallery';
</script>";
}
$stmt->close();
$conn->close();
}
?>