-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
89 lines (80 loc) · 3.46 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Halaman Utama</title>
</head>
<body>
<div class="container">
<br>
<h4>Data Produk</h4>
<!-- Buat hapus data (DELETE) -->
<?php
include "koneksi.php";
if (isset($_GET['id_produk'])) {
$id_produk = $_GET['id_produk'];
$sql = "DELETE FROM produk WHERE id_produk='$id_produk'";
$hasil = mysqli_query($kon, $sql);
if ($hasil){
header("Location: index.php");
} else {
echo "<div class='alert alert-error'>Data gagal dihapus.</div>";
}
}
?>
<!-- Buat nampilin data (READ) dan edit data (UPDATE) -->
<a href="create.php" class="btn-tambah" role="button">Tambah Data</a>
<br>
<table class="table">
<thead>
<tr class="tablehead">
<th>ID</th>
<th>Nama Produk</th>
<th>Modal</th>
<th>Harga</th>
<th>Stok</th>
</tr>
</thead>
<tbody>
<?php
// ngambil semua data dari database
$sql = "SELECT * FROM produk ORDER BY id_produk DESC";
$hasil = mysqli_query($kon, $sql);
$no = 0;
// loop buat ngambil data dari database (diambil sebagai array)
while ($data = mysqli_fetch_array($hasil)){
$no++;
?>
<tr>
<!-- nampilin data di tabel, diambil pake loop diatas -->
<td><?php echo $no ?></td>
<td><?php echo htmlspecialchars($data['nama_produk']); ?></td>
<td><?php echo htmlspecialchars($data['modal']); ?></td>
<td><?php echo htmlspecialchars($data['harga']); ?></td>
<td><?php echo htmlspecialchars($data['stok']); ?></td>
<td>
<!-- tombol edit dan hapus, yg edit ngarah ke update.php + parameter id produk -->
<a href="update.php?id_produk=<?php echo htmlspecialchars($data['id_produk']); ?>" class="btn-edit" role="button">Edit</a>
<a href="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?id_produk=<?php echo $data['id_produk']; ?>" class="btn-hapus" role="button">Hapus</a>
<!-- Untuk konfirmasi hapus data -->
<div class="modal" id="confirmDelete" style="display: none;">
<div class="modal-content">
<span class="close">×</span>
<p>Apakah Anda yakin ingin menghapus data ini?</p>
<button id="deleteConfirmBtn" class="btn-hapus">Ya, Hapus</button>
<button class="btn-cancel">Batal</button>
</div>
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<script src="script.js"></script>
</body>
</html>