UTS (DATABASE ADVANCE)
UTS DAN CONTOH SOAL DATABASE ADVANCE
Sabilla Ardani Putri (8801202211)
UTS DATABASE ADVANCE
- Buat Data Master Aplikasi Kasir dengan PHP & MongoDB yang collection terdiri dari:
- kategori_menu: _id, nama_kategori
- menu: _id, id_kategori, nama_menu, harga
- user: _id, username, password, level
- pelanggan: _id, nama, email, no_hp, alamat
- Buat list menu link yang menghubungkan antar aplikasi crud & NAMA+NIM pada header aplikasi
JAWABAN
memasukan data kedalam folder kasir di mongoDB
1. index.php
<?php
// Koneksi ke MongoDB
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$dbName = "kasir_db"; // Nama database
// Display nama dan nim di header
$nama = "Sabilla Ardani Putri";
$nim = "8801202211";
// Link untuk CRUD
$menuLinks = [
'kategori_menu' => 'Kategori Menu',
'menu' => 'Menu',
'user' => 'User',
'pelanggan' => 'Pelanggan',
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aplikasi Kasir Sabilla Ardani Putri</title>
</head>
<body>
<header>
<h1>Aplikasi Kasir Sabilla Ardani Putri</h1>
<p><?php echo $nama . " - " . $nim; ?></p>
</header>
<nav>
<ul>
<?php foreach ($menuLinks as $link => $label) { ?>
<li><a href="<?php echo $link; ?>.php"><?php echo $label; ?></a></li>
<?php } ?>
</ul>
</nav>
<hr>
<h2>Selamat Datang di Aplikasi Kasir Sabilla Ardani Putri</h2>
</body>
</html>
2. kategori_menu.php
<?php
// Koneksi ke MongoDB
$client = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$dbName = "kasir_db";
$collectionName = "kategori_menu";
// Create or Update Kategori
if (isset($_POST['submit'])) {
$nama_kategori = $_POST['nama_kategori'];
if (isset($_POST['id']) && !empty($_POST['id'])) {
// Update kategori
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['_id' => new MongoDB\BSON\ObjectID($_POST['id'])],
['$set' => ['nama_kategori' => $nama_kategori]]
);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Kategori berhasil diperbarui!";
} else {
// Buat kategori baru
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['nama_kategori' => $nama_kategori]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Kategori berhasil ditambahkan!";
}
}
// Menghapus kategori
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['_id' => new MongoDB\BSON\ObjectID($id)]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Kategori berhasil dihapus!";
}
// Menampilkan semua kategori
$query = new MongoDB\Driver\Query([]);
$cursor = $client->executeQuery("$dbName.$collectionName", $query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Kategori Menu</title>
</head>
<body>
<h1>Kategori Menu</h1>
<?php if (isset($message)) echo "<p>$message</p>"; ?>
<form method="post">
<input type="hidden" name="id" value="<?php echo isset($_GET['edit']) ? $_GET['edit'] : ''; ?>">
<input type="text" name="nama_kategori" placeholder="Nama Kategori" required>
<button type="submit" name="submit">Simpan</button>
</form>
<h2>Daftar Kategori</h2>
<ul>
<?php foreach ($cursor as $item): ?>
<li>
<?php echo $item->nama_kategori; ?>
<a href="?edit=<?php echo $item->_id; ?>">Edit</a>
<a href="?delete=<?php echo $item->_id; ?>">Hapus</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
3. menu.php
<?php
// Koneksi ke MongoDB
$client = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$dbName = "kasir_db";
$collectionName = "menu";
// Create or Update Menu
if (isset($_POST['submit'])) {
$id_kategori = $_POST['id_kategori'];
$nama_menu = $_POST['nama_menu'];
$harga = $_POST['harga'];
if (isset($_POST['id']) && !empty($_POST['id'])) {
// Update menu
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['_id' => new MongoDB\BSON\ObjectID($_POST['id'])],
['$set' => ['id_kategori' => $id_kategori, 'nama_menu' => $nama_menu, 'harga' => $harga]]
);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Menu berhasil diperbarui!";
} else {
// Buat menu baru
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['id_kategori' => $id_kategori, 'nama_menu' => $nama_menu, 'harga' => $harga]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Menu berhasil ditambahkan!";
}
}
// Menghapus menu
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['_id' => new MongoDB\BSON\ObjectID($id)]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Menu berhasil dihapus!";
}
// Menampilkan semua menu
$query = new MongoDB\Driver\Query([]);
$cursor = $client->executeQuery("$dbName.$collectionName", $query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Menu</title>
</head>
<body>
<h1>Menu</h1>
<?php if (isset($message)) echo "<p>$message</p>"; ?>
<form method="post">
<input type="hidden" name="id" value="<?php echo isset($_GET['edit']) ? $_GET['edit'] : ''; ?>">
<input type="text" name="nama_menu" placeholder="Nama Menu" required>
<input type="number" name="harga" placeholder="Harga" required>
<select name="id_kategori" required>
<option value="">Pilih Kategori</option>
<?php
// Ambil kategori untuk dropdown
$kategoriQuery = new MongoDB\Driver\Query([]);
$kategoriCursor = $client->executeQuery("$dbName.kategori_menu", $kategoriQuery);
foreach ($kategoriCursor as $kategori) {
echo "<option value='{$kategori->_id}'>{$kategori->nama_kategori}</option>";
}
?>
</select>
<button type="submit" name="submit">Simpan</button>
</form>
<h2>Daftar Menu</h2>
<ul>
<?php foreach ($cursor as $item): ?>
<li>
<?php echo $item->nama_menu; ?> (<?php echo $item->harga; ?>)
<a href="?edit=<?php echo $item->_id; ?>">Edit</a>
<a href="?delete=<?php echo $item->_id; ?>">Hapus</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
4. pelanggan.php
<?php
// Koneksi ke MongoDB
$client = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$dbName = "kasir_db";
$collectionName = "pelanggan";
// Pesan untuk notifikasi
$message = '';
// Menangani operasi Create dan Update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$nama = $_POST['nama'];
$email = $_POST['email'];
$no_hp = $_POST['no_hp'];
$alamat = $_POST['alamat'];
if (isset($_POST['id']) && !empty($_POST['id'])) {
// Update pelanggan
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['_id' => new MongoDB\BSON\ObjectID($_POST['id'])],
['$set' => ['nama' => $nama, 'email' => $email, 'no_hp' => $no_hp, 'alamat' => $alamat]]
);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Pelanggan berhasil diperbarui!";
} else {
// Buat pelanggan baru
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['nama' => $nama, 'email' => $email, 'no_hp' => $no_hp, 'alamat' => $alamat]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Pelanggan berhasil ditambahkan!";
}
}
// Menghapus pelanggan
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['_id' => new MongoDB\BSON\ObjectID($id)]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "Pelanggan berhasil dihapus!";
}
// Menampilkan semua pelanggan
$query = new MongoDB\Driver\Query([]);
$cursor = $client->executeQuery("$dbName.$collectionName", $query);
// Jika ingin mengedit, ambil data pelanggan
$pelangganToEdit = null;
if (isset($_GET['edit'])) {
$editId = $_GET['edit'];
$queryEdit = new MongoDB\Driver\Query(['_id' => new MongoDB\BSON\ObjectID($editId)]);
$pelangganToEdit = $client->executeQuery("$dbName.$collectionName", $queryEdit)->toArray()[0];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Manajemen Pelanggan</title>
</head>
<body>
<h1>Manajemen Pelanggan</h1>
<?php if ($message) echo "<p>$message</p>"; ?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $pelangganToEdit ? (string)$pelangganToEdit->_id : ''; ?>">
<input type="text" name="nama" placeholder="Nama" value="<?php echo $pelangganToEdit ? $pelangganToEdit->nama : ''; ?>" required>
<input type="email" name="email" placeholder="Email" value="<?php echo $pelangganToEdit ? $pelangganToEdit->email : ''; ?>" required>
<input type="text" name="no_hp" placeholder="No HP" value="<?php echo $pelangganToEdit ? $pelangganToEdit->no_hp : ''; ?>" required>
<input type="text" name="alamat" placeholder="Alamat" value="<?php echo $pelangganToEdit ? $pelangganToEdit->alamat : ''; ?>" required>
<button type="submit">Simpan</button>
</form>
<h2>Daftar Pelanggan</h2>
<ul>
<?php foreach ($cursor as $item): ?>
<li>
<?php echo $item->nama; ?> (<?php echo $item->email; ?>)
<a href="?edit=<?php echo $item->_id; ?>">Edit</a>
<a href="?delete=<?php echo $item->_id; ?>">Hapus</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
5. user.php
<?php
// Koneksi ke MongoDB
$client = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$dbName = "kasir_db";
$collectionName = "user";
// Pesan untuk notifikasi
$message = '';
// Menangani operasi Create dan Update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT); // Mengamankan password
$level = $_POST['level'];
if (isset($_POST['id']) && !empty($_POST['id'])) {
// Update pengguna
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['_id' => new MongoDB\BSON\ObjectID($_POST['id'])],
['$set' => ['username' => $username, 'password' => $password, 'level' => $level]]
);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "User berhasil diperbarui!";
} else {
// Buat pengguna baru
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['username' => $username, 'password' => $password, 'level' => $level]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "User berhasil ditambahkan!";
}
}
// Menghapus pengguna
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['_id' => new MongoDB\BSON\ObjectID($id)]);
$client->executeBulkWrite("$dbName.$collectionName", $bulk);
$message = "User berhasil dihapus!";
}
// Menampilkan semua pengguna
$query = new MongoDB\Driver\Query([]);
$cursor = $client->executeQuery("$dbName.$collectionName", $query);
// Jika ingin mengedit, ambil data pengguna
$userToEdit = null;
if (isset($_GET['edit'])) {
$editId = $_GET['edit'];
$queryEdit = new MongoDB\Driver\Query(['_id' => new MongoDB\BSON\ObjectID($editId)]);
$userToEdit = $manager->executeQuery("$dbName.$collectionName", $queryEdit)->toArray()[0];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Manajemen Pengguna</title>
</head>
<body>
<h1>Manajemen Pengguna</h1>
<?php if ($message) echo "<p>$message</p>"; ?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $userToEdit ? (string)$userToEdit->_id : ''; ?>">
<input type="text" name="username" placeholder="Username" value="<?php echo $userToEdit ? $userToEdit->username : ''; ?>" required>
<input type="password" name="password" placeholder="Password" required>
<select name="level" required>
<option value="">Pilih Level</option>
<option value="admin" <?php echo $userToEdit && $userToEdit->level == 'admin' ? 'selected' : ''; ?>>Admin</option>
<option value="kasir" <?php echo $userToEdit && $userToEdit->level == 'kasir' ? 'selected' : ''; ?>>Kasir</option>
</select>
<button type="submit">Simpan</button>
</form>
<h2>Daftar Pengguna</h2>
<ul>
<?php foreach ($cursor as $user): ?>
<li>
<?php echo $user->username; ?> (<?php echo $user->level; ?>)
<a href="?edit=<?php echo $user->_id; ?>">Edit</a>
<a href="?delete=<?php echo $user->_id; ?>">Hapus</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
OUTPUT
CONTOH SOAL
Download Driver MongoDB
1. Unduh driver PHP MongoDB yang sesuai dengan versi PHP
Anda.
2. Pindahkan file php_mongodb.dll ke direktori ext dalam instalasi
PHP di XAMPP (biasanya ada di C:\xampp\php\ext).
3. Edit file php.ini yang ada di C:\xampp\php\php.ini, tambahkan
baris berikut di bagian ekstensi:
Cek Installasi
1. Setelah melakukan proses tersebut, restart Apache pada xampp control panel
2. Buka browser & ketikkan
http://localhost/dashboard/phpinfo.php
3. Pastikan mongoDB sudah terinstall pada phpinfo()
Uji Koneksi MongoDB
1. Buat skrip PHP di C:\xampp\htdocs dan beri nama koneksi-mongodb.php
2. Tuliskan skrip berikut ini
3. Buka browser dan ketikkan http://localhost/mongodb/koneksi-mongodb.php
4. Jika muncul tulisan “Koneksi ke MongoDB berhasil.”, maka PHP
sudah berhasil terkoneksi dengan MongoDB
Membuat File index.php
1. Buat folder mongodb pada direktori C:\xampp\htdocs
2. Buat file index.php dan masukkan skrip pada file berikut
Tes CRUD
Komentar
Posting Komentar