forked from dewasemadi/Basic-CRUD-PHP-MySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
462 lines (376 loc) · 21.2 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php include("./config.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Belajar Dasar CRUD dengan PHP dan MySQL">
<title>Data Mahasiswa</title>
<!-- bootstrap cdn -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<!-- material icon -->
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<!-- font awesome -->
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous" />
<link rel="stylesheet" href="./css/style.css">
</head>
<body class="bg-light">
<!-- navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom" style="position: sticky !important;
top: 0 !important; z-index : 99999 !important;">
<div class="container-fluid container">
<a class="navbar-brand" href="#">Go Data</a>
<button class="navbar-toggler border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto ">
<li class="nav-item">
<a class="nav-link active text-sm-start text-center" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link btn btn-primary ms-md-4 text-white" href="#">Login</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-5">
<!-- form tambah mahasiswa -->
<div class="card mb-5">
<!-- <div class="card-header">
Latihan CRUD PHP & MySQL
</div> -->
<!-- tambah data -->
<div class="card-body">
<h3 class="card-title">Tambah Data Mahasiswa</h3>
<p class="card-text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque facere, delectus perferendis quos vel deleniti velit laborum eligendi esse quasi, nesciunt accusantium. Obcaecati impedit, deleniti totam cum recusandae rem placeat? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate deserunt magnam libero veritatis quisquam nobis! Odit cupiditate, minima consequatur expedita nulla dolor eos fuga. Blanditiis, repudiandae. Nemo harum cupiditate eum?</p>
<!-- tampilkan pesan sukses ditambah -->
<?php if (isset($_GET['status'])) : ?>
<?php
if ($_GET['status'] == 'sukses')
echo "<div class='alert alert-success alert-dismissible fade show' role='alert'>
<strong>Sukses!</strong> Data berhasil ditambahkan!
<button type='button' class='btn-close' onclick='clicking()' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
else
echo "<div class='alert alert-danger alert-dismissible fade show' role='alert'>
<strong>Ups!</strong> Data gagal ditambahkan!
<button type='button' class='btn-close' onclick='clicking()' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
?>
<?php endif; ?>
<form class="row g-3" action="tambah.php" method="POST">
<div class="col-12">
<label for="nama" class="form-label">Nama</label>
<input type="text" name="nama" class="form-control" placeholder="Steve Jobs" required>
</div>
<div class="col-md-4">
<label for="NIM" class="form-label">NIM</label>
<input type="text" name="NIM" class="form-control" placeholder="G64190021" required>
</div>
<div class="col-md-4">
<label for="Agama" class="form-label">Agama</label>
<select class="form-select" name="agama" aria-label="Default select example">
<option value="Hindu">Hindu</option>
<option value="Buddha">Buddha</option>
<option value="Islam">Islam</option>
<option value="Konghucu">Konghucu</option>
<option value="Protestan">Protestan</option>
<option value="Katolik">Katolik</option>
</select>
</div>
<div class="col-md-4">
<label for="tittle" class="form-label">Jenis Kelamin</label>
<div class="col-md-12">
<div class="form-check form-check-inline">
<label class="form-check-label" for="jenis_kelamin">Laki-Laki</label>
<input class="form-check-input" type="radio" name="jenis_kelamin" value="Laki-Laki">
</div>
<div class="form-check form-check-inline">
<label class="form-check-label" for="jenis_kelamin">Perempuan</label>
<input class="form-check-input" type="radio" name="jenis_kelamin" value="Perempuan">
</div>
</div>
</div>
<div class="col-md-6">
<label for="Jurusan" class="form-label">Jurusan</label>
<input type="text" name="jurusan" class="form-control" placeholder="Ilmu Komputer" required>
</div>
<div class="col-md-6">
<label for="IPK" class="form-label">IPK</label>
<input type="number" step=0.01 name="IPK" class=" form-control" placeholder="3.52" required>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary" value="daftar" name="tambah"><i class="fa fa-plus"></i><span class="ms-2">Tambah Data</span></button>
</div>
</form>
</div>
</div>
<!-- judul tabel -->
<h5 class="mb-3">Daftar Mahasiswa Saya</h5>
<div class="row my-3">
<div class="col-md-2 mb-3">
<select class="form-select" aria-label="Default select example">
<option selected>Tampilkan Entri</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</div>
<div class="col-md-3 ms-auto">
<div class="input-group mb-3 ms-auto">
<input type="text" class="form-control" placeholder="Cari Sesuatu..." aria-label="cari" aria-describedby="button-addon2">
<button class="btn btn-primary" type="button" id="button-addon2"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
<!-- tampilkan pesan sukses dihapus -->
<?php if (isset($_GET['hapus'])) : ?>
<?php
if ($_GET['hapus'] == 'sukses')
echo "<div class='alert alert-success alert-dismissible fade show' role='alert'>
<strong>Sukses!</strong> Data berhasil dihapus!
<button type='button' class='btn-close' onclick='clicking()' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
else
echo "<div class='alert alert-danger alert-dismissible fade show' role='alert'>
<strong>Ups!</strong> Data gagal dihapus!
<button type='button' class='btn-close' onclick='clicking()' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
?>
<?php endif; ?>
<!-- tampilkan pesan sukses di edit -->
<?php if (isset($_GET['update'])) : ?>
<?php
if ($_GET['update'] == 'sukses')
echo "<div class='alert alert-success alert-dismissible fade show' role='alert'>
<strong>Sukses!</strong> Data berhasil diupdate!
<button type='button' class='btn-close' onclick='clicking()' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
else
echo "<div class='alert alert-danger alert-dismissible fade show' role='alert'>
<strong>Ups!</strong> Data gagal diupdate!
<button type='button' class='btn-close' onclick='clicking()' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
?>
<?php endif; ?>
<!-- tabel -->
<div class="table-responsive mb-5 card">
<?php
echo "<div class='card-body'>";
echo "<table class='table table-hover align-middle bg-white'>";
echo "<thead>";
echo "<tr>";
echo "<th scope='col' class='text-center'>No</th>";
echo "<th scope='col'>Nama</th>";
echo "<th scope='col'>NIM</th>";
echo "<th scope='col'>Jenis Kelamin</th>";
echo "<th scope='col'>Jurusan</th>";
echo "<th scope='col'>Agama</th>";
echo "<th scope='col'>IPK</th>";
echo "<th scope='col' class='text-center'>Opsi</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$batas = 10;
$halaman = isset($_GET['halaman']) ? (int)$_GET['halaman'] : 1;
$halaman_awal = ($halaman > 1) ? ($halaman * $batas) - $batas : 0;
$previous = $halaman - 1;
$next = $halaman + 1;
$data = mysqli_query($db, "SELECT * FROM mahasiswa");
$jumlah_data = mysqli_num_rows($data);
$total_halaman = ceil($jumlah_data / $batas);
$data_mhs = mysqli_query($db, "SELECT * FROM mahasiswa LIMIT $halaman_awal, $batas");
$no = $halaman_awal + 1;
// $sql = "SELECT * FROM mahasiswa";
// $query = mysqli_query($db, $sql);
while ($mahasiswa = mysqli_fetch_array($data_mhs)) {
echo "<tr>";
echo "<td style='display:none'>" . $mahasiswa['id'] . "</td>";
echo "<td class='text-center'>" . $no++ . "</td>";
echo "<td>" . $mahasiswa['nama'] . "</td>";
echo "<td>" . $mahasiswa['NIM'] . "</td>";
echo "<td>" . $mahasiswa['jenis_kelamin'] . "</td>";
echo "<td>" . $mahasiswa['jurusan'] . "</td>";
echo "<td>" . $mahasiswa['agama'] . "</td>";
echo "<td>" . $mahasiswa['IPK'] . "</td>";
echo "<td class='text-center'>";
echo "<button type='button' class='btn btn-primary editButton pad m-1'><span class='material-icons align-middle'>edit</span></button>";
// tombol hapus
echo "
<!-- Button trigger modal -->
<button type='button' class='btn btn-danger deleteButton pad m-1'><span class='material-icons align-middle'>delete</span></button>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
if ($jumlah_data == 0) {
echo "<p class='text-center'>Tidak ada data yang tersedia pada tabel ini</p>";
} else {
echo "<p>Total $jumlah_data entri</p>";
}
echo "</div>";
?>
</div>
<!-- pagination -->
<nav class="mt-5 mb-5">
<ul class="pagination justify-content-center">
<li class="page-item">
<a class="page-link" <?php echo ($halaman > 1) ? "href='?halaman=$previous'" : "" ?>><i class="fa fa-chevron-left"></i></a>
</li>
<?php
for ($x = 1; $x <= $total_halaman; $x++) {
?>
<li class="page-item"><a class="page-link" href="?halaman=<?php echo $x ?>"><?php echo $x; ?></a></li>
<?php
}
?>
<li class="page-item">
<a class="page-link" <?php echo ($halaman < $total_halaman) ? "href='?halaman=$next'" : "" ?>><i class="fa fa-chevron-right"></i></a>
</li>
</ul>
</nav>
<!-- Modal Edit-->
<div class='modal fade' style="top:58px !important;" id='editModal' tabindex='-1' aria-labelledby='exampleModalLabel' aria-hidden='true'>
<div class='modal-dialog' style="margin-bottom:100px !important;">
<div class='modal-content'>
<div class='modal-header'>
<h5 class='modal-title' id='exampleModalLabel'>Edit Data Mahasiswa</h5>
<button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button>
</div>
<?php
$sql = "SELECT * FROM mahasiswa";
$query = mysqli_query($db, $sql);
$mahasiswa = mysqli_fetch_array($query);
?>
<form action='edit.php' method='POST'>
<div class='modal-body text-start'>
<input type='hidden' name='edit_id' id='edit_id'>
<div class="col-12 mb-3">
<label for="edit_nama" class="form-label">Nama</label>
<input type="text" name="edit_nama" id="edit_nama" class="form-control" placeholder="Steve Jobs" required>
</div>
<div class="col-12 mb-3">
<label for="edit_NIM" class="form-label">NIM</label>
<input type="text" name="edit_NIM" id="edit_NIM" class="form-control" placeholder="G64190021" required>
</div>
<div class="col-12 mb-3">
<label for="edit_agama" class="form-label">Agama</label>
<select class="form-select" name="edit_agama" id="edit_agama" aria-label="Default select example">
<option value="Hindu">Hindu</option>
<option value="Buddha">Buddha</option>
<option value="Islam">Islam</option>
<option value="Konghucu">Konghucu</option>
<option value="Protestan">Protestan</option>
<option value="Katolik">Katolik</option>
</select>
</div>
<div class="col-12 mb-3">
<label for="edit_jenis_kelamin" class="form-label">Jenis Kelamin</label>
<div class="col-md-12" id="gender">
<div class="form-check form-check-inline">
<label class="form-check-label" for="jenis_kelamin">
<input class="form-check-input" type="radio" name="edit_jenis_kelamin" value="Laki-Laki" id="cowok">Laki-Laki</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label" for="jenis_kelamin">
<input class="form-check-input" type="radio" name="edit_jenis_kelamin" value="Perempuan" id="cewek">Perempuan</label>
</div>
</div>
</div>
<div class="col-12 mb-3">
<label for="edit_urusan" class="form-label">Jurusan</label>
<input type="text" name="edit_jurusan" class="form-control" id="edit_jurusan" placeholder="Ilmu Komputer" required>
</div>
<div class="col-12 mb-3">
<label for="edit_ipk" class="form-label">IPK</label>
<input type="number" step=0.01 name="edit_ipk" id="edit_ipk" class=" form-control" placeholder="3.52" required>
</div>
</div>
<div class='modal-footer'>
<button type='submit' name='edit_data' class='btn btn-primary'>Simpan</button>
</div>
</form>
</div>
</div>
</div>
<!-- Modal Delete-->
<div class='modal fade' style="top:58px !important;" id='deleteModal' tabindex='-1' aria-labelledby='exampleModalLabel' aria-hidden='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<h5 class='modal-title' id='exampleModalLabel'>Konfirmasi</h5>
<button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button>
</div>
<form action='hapus.php' method='POST'>
<div class='modal-body text-start'>
<input type='hidden' name='delete_id' id='delete_id'>
<p>Yakin ingin menghapus data ini?</p>
</div>
<div class='modal-footer'>
<button type='submit' name='deletedata' class='btn btn-primary'>Hapus</button>
</div>
</form>
</div>
</div>
</div>
<!-- tutup container -->
</div>
<!-- Jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Javascript bule with popper bootstrap -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<!-- sweet alert -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<!-- edit function -->
<script>
$(document).ready(function() {
$('.editButton').on('click', function() {
$('#editModal').modal('show');
$tr = $(this).closest('tr');
var data = $tr.children("td").map(function() {
return $(this).text();
}).get();
console.log(data);
$('#edit_id').val(data[0]);
// gak dipake, karena cuma increment number
// $('#no').val(data[1]);
$('#edit_nama').val(data[2]);
$('#edit_NIM').val(data[3]);
$('#gender').val(data[4]);
// jenis kelamin checked
if (data[4] == "Laki-Laki") {
$("#cowok").prop("checked", true);
} else {
$("#cewek").prop("checked", true);
}
$('#edit_jurusan').val(data[5]);
$('#edit_agama').val(data[6]);
$('#edit_ipk').val(data[7]);
});
});
</script>
<!-- delete function -->
<script>
$(document).ready(function() {
$('.deleteButton').on('click', function() {
$('#deleteModal').modal('show');
$tr = $(this).closest('tr');
var data = $tr.children("td").map(function() {
return $(this).text();
}).get();
console.log(data);
$('#delete_id').val(data[0]);
});
});
</script>
<script>
function clicking() {
window.location.href = './index.php';
}
</script>
</body>
</html>