Setelah langkah diatas berhasil, kita akan mencoba merapikan kodingan agar modular dengan menambah function, silahkan tambahkan file function.php, isi code nya seperti dibawah ini :
<?php
$db = mysqli_connect("localhost", "root", "", "belajarphp");
function query($query)
{
global $db;
$result = mysqli_query($db, $query);
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
Setelah itu tambahkan code pada index.php seperti dibawah ini
<?php
// Koneksi ke database
require 'function.php';
$mahasiswa = query("SELECT * FROM mahasiswa");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Data Mahasiswa</title>
</head>
<body>
<h1>Daftar Mahasiswa</h1>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>No.</th>
<th>Aksi</th>
<th>Photo</th>
<th>Nama</th>
<th>NPM</th>
<th>Jurusan</th>
<th>Email</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($mahasiswa as $row) : ?>
<tr>
<td> <?= $i; ?> </td>
<td>
<a href="">Ubah</a> |
<a href="">Hapus</a>
</td>
<td><img src="img/<?php echo $row["gambar"] ?>" width="50"></td>
<td><?php echo $row["nama"] ?></td>
<td><?php echo $row["npm"] ?></td>
<td><?php echo $row["jurusan"] ?></td>
<td><?php echo $row["email"] ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
</table>
</body>
</html>
Data yang tampil sama persis seperti data sebelumnya, hanya saja sekarang kita membuat kodingan sedikit lebih modular.




