Skip to content

Commit e3d795c

Browse files
committed
added delete function
1 parent f5ab1ae commit e3d795c

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed

user_account/delete.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
include "../connection.php";
3+
include "../admin_privilege.php";
4+
5+
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["id"])) {
6+
$userId = $_GET["id"];
7+
8+
// Periksa apakah pengguna dengan ID tertentu ada di database
9+
$checkUserQuery = "SELECT * FROM user_account WHERE account_id = ?";
10+
$stmtCheckUser = $conn->prepare($checkUserQuery);
11+
$stmtCheckUser->bind_param("i", $userId);
12+
$stmtCheckUser->execute();
13+
$resultCheckUser = $stmtCheckUser->get_result();
14+
15+
if ($resultCheckUser->num_rows > 0) {
16+
// Data pengguna ditemukan, lakukan penghapusan
17+
$deleteUserQuery = "DELETE FROM user_account WHERE account_id = ?";
18+
$stmtDeleteUser = $conn->prepare($deleteUserQuery);
19+
$stmtDeleteUser->bind_param("i", $userId);
20+
$stmtDeleteUser->execute();
21+
$stmtDeleteUser->close();
22+
23+
// Redirect ke halaman list setelah penghapusan
24+
header("Location: list.php");
25+
exit();
26+
} else {
27+
// Data pengguna tidak ditemukan, mungkin ID tidak valid
28+
echo "Data pengguna tidak ditemukan.";
29+
}
30+
31+
$stmtCheckUser->close();
32+
} else {
33+
// Jika tidak ada ID yang diterima atau request bukan GET, tampilkan pesan kesalahan
34+
echo "ID pengguna tidak valid atau request tidak sesuai.";
35+
}
36+
?>

user_account/list.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@
3030
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.7/css/jquery.dataTables.min.css">
3131
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.4.2/css/buttons.dataTables.min.css">
3232
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.7/css/dataTables.bootstrap4.min.css">
33+
<!-- Sweetalert2 -->
34+
<link rel="stylesheet" href="../assets/adminlte/plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css">
3335

3436
<style>
3537
.lebar-kolom1 {
3638
width: 5%;
3739
}
3840

3941
.lebar-kolom2 {
40-
width: 45%;
42+
width: 40%;
4143
}
4244

4345
.lebar-kolom3 {
@@ -57,7 +59,7 @@
5759
}
5860

5961
.lebar-kolom7 {
60-
width: 10%;
62+
width: 15%;
6163
}
6264

6365
.card-padding {
@@ -116,7 +118,7 @@
116118
<!-- /.card-header -->
117119
<div class="card-body p-0">
118120
<div class="table-responsive card-padding">
119-
<table id="tablePerusahaan" class="table table order-list table-striped table-bordered">
121+
<table id="tableUser" class="table table order-list table-striped table-bordered">
120122
<thead>
121123
<tr>
122124
<th class="text-center lebar-kolom1">ID</th>
@@ -150,9 +152,10 @@
150152
?>
151153
</td>
152154
<td class="text-center">
153-
<div class="col">
154-
<a href='edit.php?id=<?php echo $row["account_id"]; ?>' class="btn btn-info btn-block">Edit</a>
155+
<div style="display: inline-block;">
156+
<a href='edit.php?id=<?php echo $row["account_id"]; ?>' class="btn btn-info"><i class="fas fa-pencil"></i></a>
155157
</div>
158+
<a href='#' class='btn btn-danger delete-btn' data-id='<?php echo $row["account_id"]; ?>'><i class="fas fa-trash"></i></a>
156159
</td>
157160
</tr>
158161
<?php
@@ -204,11 +207,36 @@
204207
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.html5.min.js"></script>
205208
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.print.min.js"></script>
206209
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.bootstrap4.min.js"></script>
210+
<!-- SweetAlert2 Toast -->
211+
<script src="../assets/adminlte/plugins/sweetalert2/sweetalert2.min.js"></script>
207212

208213
<!-- Page specific script -->
209214
<script>
210215
$(document).ready(function() {
211-
var table = $('#tablePerusahaan').DataTable({
216+
217+
$('#tableUser').on('click', '.delete-btn', function(e) {
218+
e.preventDefault();
219+
var userId = $(this).data('id');
220+
221+
// Tampilkan konfirmasi SweetAlert
222+
Swal.fire({
223+
title: 'Apakah Anda yakin?',
224+
text: 'Data pengguna ini akan dihapus!',
225+
icon: 'warning',
226+
showCancelButton: true,
227+
confirmButtonColor: '#d33',
228+
cancelButtonColor: '#3085d6',
229+
confirmButtonText: 'Ya, hapus!',
230+
cancelButtonText: 'Batal'
231+
}).then((result) => {
232+
if (result.isConfirmed) {
233+
// Jika dikonfirmasi, lakukan penghapusan
234+
window.location.href = 'delete.php?id=' + userId;
235+
}
236+
});
237+
});
238+
239+
var table = $('#tableUser').DataTable({
212240
responsive: true,
213241
language: {
214242
lengthMenu: 'Tampilkan _MENU_ data per halaman',
@@ -224,7 +252,7 @@
224252
'pageLength',
225253
{
226254
extend: 'copy',
227-
title: 'List Perusahaan',
255+
title: 'List User',
228256
exportOptions: {
229257
columns: ':visible:not(.aksi-column)'
230258
}
@@ -236,28 +264,28 @@
236264
},
237265
{
238266
extend: 'csv',
239-
title: 'List Perusahaan',
267+
title: 'List User',
240268
exportOptions: {
241269
columns: ':visible:not(.aksi-column)'
242270
}
243271
},
244272
{
245273
extend: 'excel',
246-
title: 'List Perusahaan',
274+
title: 'List User',
247275
exportOptions: {
248276
columns: ':visible:not(.aksi-column)'
249277
}
250278
},
251279
{
252280
extend: 'pdf',
253-
title: 'List Perusahaan',
281+
title: 'List User',
254282
exportOptions: {
255283
columns: ':visible:not(.aksi-column)'
256284
}
257285
},
258286
{
259287
extend: 'print',
260-
title: 'List Perusahaan',
288+
title: 'List User',
261289
exportOptions: {
262290
columns: ':visible:not(.aksi-column)'
263291
}

0 commit comments

Comments
 (0)