เทคโนโลยี: PHP + MySQL + JavaScript (QR Code)

ฟีเจอร์ของระบบ

  1. 📖 ลูกค้าสแกน QR Code เพื่อเปิดเมนูออนไลน์
  2. 🛒 ลูกค้าเลือกอาหารและยืนยันออเดอร์
  3. 📦 เก็บคำสั่งซื้อเข้า Database
  4. 🍽 ครัวดูรายการอาหารที่สั่ง
  5. 🔄 อัปเดตสถานะอาหาร (กำลังเตรียม / เสิร์ฟแล้ว)
  6. 📊 ดูประวัติออเดอร์

สร้างฐานข้อมูล MySQL

CREATE DATABASE restaurant_db;
USE restaurant_db;

-- ตารางเมนูอาหาร
CREATE TABLE menu (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    price DECIMAL(10,2) NOT NULL
);

-- ตารางคำสั่งซื้อ
CREATE TABLE orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    table_number INT NOT NULL,
    status ENUM('pending', 'preparing', 'served') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- ตารางรายการอาหารที่สั่ง
CREATE TABLE order_items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT,
    menu_id INT,
    quantity INT NOT NULL,
    FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
    FOREIGN KEY (menu_id) REFERENCES menu(id) ON DELETE CASCADE
);

2. เชื่อมต่อฐานข้อมูล (db.php)

<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "restaurant_db";

$conn = new mysqli($host, $user, $pass, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

สร้าง QR Code สำหรับโต๊ะ (generate_qr.php)

<?php
include 'db.php';

require 'vendor/autoload.php';
use Endroid\QrCode\QrCode;

$table_number = isset($_GET['table']) ? intval($_GET['table']) : 1;
$url = "http://yourwebsite.com/menu.php?table=" . $table_number;

$qrCode = new QrCode($url);
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

📌 ติดตั้งไลบรารี QR Code

composer require endroid/qr-code

เมนูอาหาร (menu.php)

<?php
include 'db.php';

$table_number = isset($_GET['table']) ? intval($_GET['table']) : 1;
$menu = $conn->query("SELECT * FROM menu");
?>

<!DOCTYPE html>
<html>
<head>
    <title>เมนูอาหาร</title>
</head>
<body>
    <h2>เมนูอาหาร (โต๊ะที่ <?php echo $table_number; ?>)</h2>
    <form action="order.php" method="post">
        <input type="hidden" name="table_number" value="<?php echo $table_number; ?>">
        <table border="1">
            <tr>
                <th>ชื่ออาหาร</th>
                <th>ราคา</th>
                <th>จำนวน</th>
            </tr>
            <?php while ($row = $menu->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row['name']; ?></td>
                    <td><?php echo number_format($row['price'], 2); ?> บาท</td>
                    <td>
                        <input type="number" name="quantity[<?php echo $row['id']; ?>]" min="0" value="0">
                    </td>
                </tr>
            <?php endwhile; ?>
        </table>
        <br>
        <button type="submit">สั่งอาหาร</button>
    </form>
</body>
</html>

5. บันทึกคำสั่งซื้อ (order.php)

<?php
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $table_number = $_POST['table_number'];
    $quantities = $_POST['quantity'];

    $stmt = $conn->prepare("INSERT INTO orders (table_number) VALUES (?)");
    $stmt->bind_param("i", $table_number);
    $stmt->execute();
    $order_id = $stmt->insert_id;

    foreach ($quantities as $menu_id => $quantity) {
        if ($quantity > 0) {
            $stmt = $conn->prepare("INSERT INTO order_items (order_id, menu_id, quantity) VALUES (?, ?, ?)");
            $stmt->bind_param("iii", $order_id, $menu_id, $quantity);
            $stmt->execute();
        }
    }

    echo "สั่งอาหารสำเร็จ! หมายเลขโต๊ะ: $table_number";
    echo "<br><a href='menu.php?table=$table_number'>กลับไปที่เมนู</a>";
}
?>

แสดงออเดอร์ในครัว (kitchen.php)

<?php
include 'db.php';

$result = $conn->query("
    SELECT orders.id, orders.table_number, orders.status, menu.name, order_items.quantity 
    FROM orders 
    JOIN order_items ON orders.id = order_items.order_id 
    JOIN menu ON order_items.menu_id = menu.id
    WHERE orders.status != 'served'
    ORDER BY orders.id ASC
");
?>

<!DOCTYPE html>
<html>
<head>
    <title>รายการอาหารที่สั่ง</title>
</head>
<body>
    <h2>รายการอาหารที่สั่ง</h2>
    <table border="1">
        <tr>
            <th>หมายเลขโต๊ะ</th>
            <th>ชื่ออาหาร</th>
            <th>จำนวน</th>
            <th>สถานะ</th>
            <th>การจัดการ</th>
        </tr>
        <?php while ($row = $result->fetch_assoc()): ?>
            <tr>
                <td><?php echo $row['table_number']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['quantity']; ?></td>
                <td><?php echo $row['status']; ?></td>
                <td>
                    <a href="update_status.php?id=<?php echo $row['id']; ?>&status=preparing">เริ่มทำ</a> |
                    <a href="update_status.php?id=<?php echo $row['id']; ?>&status=served">เสิร์ฟแล้ว</a>
                </td>
            </tr>
        <?php endwhile; ?>
    </table>
</body>
</html>

อัปเดตสถานะอาหาร (update_status.php)

<?php
include 'db.php';

if (isset($_GET['id']) && isset($_GET['status'])) {
    $id = $_GET['id'];
    $status = $_GET['status'];

    $stmt = $conn->prepare("UPDATE orders SET status=? WHERE id=?");
    $stmt->bind_param("si", $status, $id);
    $stmt->execute();
}

header("Location: kitchen.php");
exit();
?>

วิธีใช้งาน

  1. สร้างฐานข้อมูล restaurant_db และนำ SQL ไปสร้างตาราง
  2. ติดตั้ง Composer และ QR Code Library
composer require endroid/qr-code
  1. เปิด generate_qr.php?table=1 เพื่อสร้าง QR Code สำหรับโต๊ะที่ 1
  2. ลูกค้าสแกน QR Code และเลือกอาหารจาก menu.php?table=1
  3. เปิด kitchen.php เพื่อตรวจสอบรายการอาหาร
  4. อัปเดตสถานะคำสั่งซื้อใน update_status.php

,


ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *