Skip to content

kelasphp-kafekoding/form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kantin Bu Wati - Sistem Pemesanan Sederhana

Aplikasi pemesanan makanan dan minuman berbasis PHP untuk kantin.

Langkah-Langkah Membuat Project

Langkah 1: Download Bootstrap

  1. Buka https://getbootstrap.com/
  2. Klik tombol Download
  3. Download "Compiled CSS and JS"
  4. Extract file zip yang sudah didownload

Langkah 2: Buat Folder Project

  1. Buat folder baru dengan nama form
  2. Di dalam folder form, buat folder bootstrap
  3. Copy folder css dan js dari Bootstrap yang sudah di-extract ke folder bootstrap
  4. Di dalam folder form, buat folder action

Langkah 3: Buat File stock.php

Buat file stock.php di folder form. File ini berisi data menu kantin.

<?php
// Data stock dan harga menu kantin Bu Wati

$menuKantin = array(
    array(
        'id' => 1,
        'nama' => 'Kopi',
        'harga' => 5000,
        'stock' => 50
    ),
    array(
        'id' => 2,
        'nama' => 'Teh',
        'harga' => 3000,
        'stock' => 40
    ),
    array(
        'id' => 3,
        'nama' => 'Susu',
        'harga' => 7000,
        'stock' => 30
    ),
    array(
        'id' => 4,
        'nama' => 'Nasi Goreng',
        'harga' => 15000,
        'stock' => 25
    ),
    array(
        'id' => 5,
        'nama' => 'Mie Goreng',
        'harga' => 12000,
        'stock' => 20
    ),
    array(
        'id' => 6,
        'nama' => 'Nasi Uduk',
        'harga' => 10000,
        'stock' => 15
    ),
    array(
        'id' => 7,
        'nama' => 'Roti Bakar',
        'harga' => 8000,
        'stock' => 35
    ),
    array(
        'id' => 8,
        'nama' => 'Gorengan',
        'harga' => 2000,
        'stock' => 600
    )
);

Cara menambah menu baru:

Tambahkan array baru di dalam $menuKantin. Contoh menambah menu "Es Jeruk":

array(
    'id' => 9,
    'nama' => 'Es Jeruk',
    'harga' => 5000,
    'stock' => 30
)

Pastikan id tidak sama dengan menu yang sudah ada.

Langkah 4: Buat File action/functions.php

Buat file functions.php di folder action. File ini berisi fungsi-fungsi helper.

<?php
// Functions untuk operasi kantin

function getMenuById($id) {
    global $menuKantin;
    foreach ($menuKantin as $menu) {
        if ($menu['id'] == $id) {
            return $menu;
        }
    }
    return null;
}

function formatRupiah($angka) {
    return 'Rp ' . number_format($angka, 0, ',', '.');
}

Cara menambah fungsi baru:

Tambahkan fungsi di bawah fungsi yang sudah ada. Contoh menambah fungsi hitung diskon:

function hitungDiskon($harga, $persen) {
    return $harga - ($harga * $persen / 100);
}

Langkah 5: Buat File action/form_handler.php

Buat file form_handler.php di folder action. File ini untuk mengambil dan validasi data form.

<?php
// Ambil dan validasi data dari form

// Validasi method
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: index.php');
    exit;
}

// Ambil data dari form
$nama_pembeli = isset($_POST['nama_pembeli']) ? trim($_POST['nama_pembeli']) : '';
$menu_ids = isset($_POST['menu']) ? $_POST['menu'] : array();
$jumlah = isset($_POST['jumlah']) ? $_POST['jumlah'] : array();
$catatan = isset($_POST['catatan']) ? trim($_POST['catatan']) : '';

// Validasi input
if (empty($nama_pembeli)) {
    die('Nama pembeli harus diisi!');
}

if (empty($menu_ids)) {
    die('Pilih minimal 1 menu!');
}

Cara menambah validasi baru:

Tambahkan pengecekan di bawah validasi yang sudah ada. Contoh validasi nama minimal 3 karakter:

if (strlen($nama_pembeli) < 3) {
    die('Nama pembeli minimal 3 karakter!');
}

Langkah 6: Buat File action/process_order.php

Buat file process_order.php di folder action. File ini untuk memproses pesanan.

<?php
// Proses pesanan dan hitung total

$pesanan = array();
$total = 0;

foreach ($menu_ids as $menu_id) {
    $menu = getMenuById($menu_id);
    if ($menu) {
        $qty = isset($jumlah[$menu_id]) ? (int)$jumlah[$menu_id] : 1;
        
        // Validasi jumlah
        if ($qty < 1) $qty = 1;
        if ($qty > $menu['stock']) $qty = $menu['stock'];
        
        $subtotal = $menu['harga'] * $qty;
        $total += $subtotal;
        
        $pesanan[] = array(
            'nama' => $menu['nama'],
            'harga' => $menu['harga'],
            'jumlah' => $qty,
            'subtotal' => $subtotal
        );
    }
}

// Generate invoice
$nomor_invoice = 'INV-' . date('Ymd') . '-' . rand(1000, 9999);
$tanggal = date('d/m/Y H:i:s');

Cara mengubah format nomor invoice:

Ubah baris $nomor_invoice. Contoh format berbeda:

$nomor_invoice = 'KBW-' . date('dmY') . '-' . rand(100, 999);

Langkah 7: Buat File index.php

Buat file index.php di folder form. Ini adalah halaman form pemesanan.

<?php
// Load data menu
require_once 'stock.php';
require_once 'action/functions.php';

$pageTitle = 'Kantin Bu Wati';
$menus = $menuKantin;
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-4">
        <h2><?php echo $pageTitle; ?></h2>
        <p>Silakan pilih menu dan jumlah pesanan</p>
        <hr>

        <form action="dash.php" method="POST">
            <div class="mb-3">
                <label>Nama Pembeli</label>
                <input type="text" class="form-control" name="nama_pembeli" required>
            </div>

            <div class="mb-3">
                <label>Pilih Menu</label>
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Pilih</th>
                                <th>Menu</th>
                                <th>Harga</th>
                                <th>Stock</th>
                                <th>Jumlah</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($menus as $menu): ?>
                            <tr>
                                <td><input type="checkbox" name="menu[]" value="<?php echo $menu['id']; ?>"></td>
                                <td><?php echo $menu['nama']; ?></td>
                                <td><?php echo formatRupiah($menu['harga']); ?></td>
                                <td><?php echo $menu['stock']; ?></td>
                                <td><input type="number" class="form-control form-control-sm" name="jumlah[<?php echo $menu['id']; ?>]" value="1" min="1" max="<?php echo $menu['stock']; ?>" style="width:70px"></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="mb-3">
                <label>Catatan (Opsional)</label>
                <textarea class="form-control" name="catatan" rows="2"></textarea>
            </div>

            <button type="submit" class="btn btn-primary">Pesan Sekarang</button>
        </form>
    </div>
</body>
</html>

Cara menambah field baru di form:

Tambahkan di dalam tag <form>. Contoh menambah field nomor meja:

<div class="mb-3">
    <label>Nomor Meja</label>
    <input type="number" class="form-control" name="nomor_meja" min="1" max="20">
</div>

Langkah 8: Buat File dash.php

Buat file dash.php di folder form. Ini adalah halaman invoice.

<?php
require_once 'stock.php';
require_once 'action/functions.php';
require_once 'action/form_handler.php';
require_once 'action/process_order.php';

$pageTitle = 'Invoice - Kantin Bu Wati';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-4">
        <h2>Invoice Pesanan</h2>
        <p>Kantin Bu Wati</p>
        <hr>

        <p><strong>No. Invoice:</strong> <?php echo $nomor_invoice; ?></p>
        <p><strong>Tanggal:</strong> <?php echo $tanggal; ?></p>
        <p><strong>Nama Pembeli:</strong> <?php echo htmlspecialchars($nama_pembeli); ?></p>

        <div class="table-responsive">
        <table class="table table-bordered mt-3">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Menu</th>
                    <th>Harga</th>
                    <th>Jumlah</th>
                    <th>Subtotal</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1; foreach ($pesanan as $item): ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $item['nama']; ?></td>
                    <td><?php echo formatRupiah($item['harga']); ?></td>
                    <td><?php echo $item['jumlah']; ?></td>
                    <td><?php echo formatRupiah($item['subtotal']); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="4"><strong>Total</strong></td>
                    <td><strong><?php echo formatRupiah($total); ?></strong></td>
                </tr>
            </tfoot>
        </table>
        </div>

        <?php if (!empty($catatan)): ?>
        <p><strong>Catatan:</strong> <?php echo htmlspecialchars($catatan); ?></p>
        <?php endif; ?>

        <a href="index.php" class="btn btn-primary">Pesan Lagi</a>
        <button onclick="window.print()" class="btn btn-secondary">Cetak</button>
    </div>
</body>
</html>

Cara menampilkan field baru di invoice:

Tambahkan di bagian info invoice. Contoh menampilkan nomor meja:

<p><strong>Nomor Meja:</strong> <?php echo $nomor_meja; ?></p>

Jangan lupa tambahkan juga di form_handler.php:

$nomor_meja = isset($_POST['nomor_meja']) ? (int)$_POST['nomor_meja'] : 0;

Alur Kerja Aplikasi

1. Pembeli buka index.php
         ↓
2. Isi nama, pilih menu, isi jumlah
         ↓
3. Klik tombol "Pesan Sekarang"
         ↓
4. Data dikirim ke dash.php (method POST)
         ↓
5. form_handler.php ambil & validasi data
         ↓
6. process_order.php hitung total
         ↓
7. Invoice ditampilkan

Tips untuk Peserta

  1. Pahami alur data - Ikuti bagaimana data mengalir dari form ke invoice
  2. Baca komentar - Setiap file ada komentar penjelasan
  3. Coba modifikasi - Tambah menu baru di stock.php, lihat hasilnya
  4. Gunakan var_dump() - Untuk debug dan lihat isi variabel

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages