Skip to content

Instantly share code, notes, and snippets.

@erowsika
Created November 23, 2021 14:31
Show Gist options
  • Save erowsika/48825e6b5aea7fb6b2008b42ba8fcc41 to your computer and use it in GitHub Desktop.
Save erowsika/48825e6b5aea7fb6b2008b42ba8fcc41 to your computer and use it in GitHub Desktop.
<?php
$username = "blabla";
$pass = "3CVzq8MmV8U3TXDL";
$host = "localhost";
$db = "db_arsip";
$connect = mysqli_connect($host, $username, $pass, $db) or die(mysqli_error($connect));
// func mysqli_escape_string prevent sqli injection security purpose
// @ keyword is suppress error message (hide it) if that variable doesn't have value which some time it's
$q = [
'no_arsip' => @mysqli_escape_string($connect, $_POST['q1']),
'judul' => @mysqli_escape_string($connect, $_POST['q2']),
'klasifikasi' => @mysqli_escape_string($connect, $_POST['q3']),
];
// ilustrasi isi table arsip di database
// | id | no_arsip | judul | klasifikasi |
// |----|----------|----------------|-------------|
// | 1 | 1 | Aku makan nasi | makan |
// | 2 | 2 | Aku minum teh | nasi |
// populate select no_arsip, judul, klasifikasi
$no_arsip = $judul = $klasifikasi = [];
$populate = mysqli_query($connect, "SELECT * FROM arsip;");
while ($row = mysqli_fetch_array($populate)) {
$no_arsip[] = $row['no_arsip'];
$judul[] = $row['judul'];
$klasifikasi[] = $row['klasifikasi'];
}
?>
<!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>Document</title>
</head>
<body>
<main>
<form action="" method="POST">
<div>
<label">No Arsip</label>
<select name="q1">
<?php foreach ($no_arsip as $_no_arsip) { ?>
<option value="<?php echo $_no_arsip; ?>"><?php echo $_no_arsip; ?></option>
<?php } ?>
</select>
</div>
<div>
<label">Judul</label>
<select name="q2">
<?php foreach ($judul as $_judul) { ?>
<option value="<?php echo $_judul; ?>"><?php echo $_judul; ?></option>
<?php } ?>
</select>
</div>
<div>
<label">Klasifikasi</label>
<select name="q3">
<?php foreach ($klasifikasi as $_klasifikasi) { ?>
<option value="<?php echo $_klasifikasi; ?>"><?php echo $_klasifikasi; ?></option>
<?php } ?>
</select>
</div>
<div>
<button type="submit">Cari</button>
</div>
</form>
<table>
<thead>
<tr>
<th>#</th>
<th>No. Arsip</th>
<th>Judul</th>
<th>Klasifikasi</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$query = "SELECT * FROM arsip";
// remove null value
$filtered = array_filter($q);
// build query where
array_walk($filtered, function(&$value, $key) {
$value = "{$key}='{$value}'";
});
// join into single query
$query = $query . (count($filtered) ? ' WHERE ' : ' ') . implode('AND ', $filtered);
$result = mysqli_query($connect, $query);
while ($record = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $record['no_arsip']; ?></td>
<td><?php echo $record['judul']; ?></td>
<td><?php echo $record['klasifikasi']; ?></td>
</tr>
<?php $no++; ?>
<?php } ?>
</tbody>
</table>
</main>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment