ฟีเจอร์ของระบบ:
- 📖 แสดงเมนูอาหาร ให้ลูกค้าเลือก
- 🛒 ลูกค้าสั่งอาหารจากโต๊ะ ผ่านหน้าเว็บ
- 📦 จัดเก็บรายการสั่งอาหารลงฐานข้อมูล
- 🎯 สถานะการสั่งอาหาร (กำลังเตรียม / เสร็จแล้ว)
- 📢 แสดงรายการสั่งอาหารสำหรับครัวและแคชเชียร์
สร้างฐานข้อมูลและตาราง MySQL
ให้สร้างฐานข้อมูลและตารางที่จำเป็นด้วย SQL ด้านล่าง:
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
);
เชื่อมต่อฐานข้อมูล (db.php)
ไฟล์นี้ใช้สำหรับเชื่อมต่อกับฐานข้อมูล MySQL
<?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);
}
?>
3. แสดงเมนูอาหาร (menu.php)
ไฟล์นี้ใช้แสดงรายการอาหารที่มีอยู่ในเมนู และให้ลูกค้าเลือกสั่ง:
<?php
include 'db.php';
// ดึงรายการอาหารจากฐานข้อมูล
$result = $conn->query("SELECT * FROM menu");
?>
<!DOCTYPE html>
<html>
<head>
<title>เมนูอาหาร</title>
</head>
<body>
<h2>เมนูอาหาร</h2>
<form action="order.php" method="post">
<label>หมายเลขโต๊ะ:</label>
<input type="number" name="table_number" required><br><br>
<table border="1">
<tr>
<th>ชื่ออาหาร</th>
<th>ราคา</th>
<th>จำนวน</th>
</tr>
<?php while ($row = $result->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>
บันทึกคำสั่งซื้อ (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'>กลับไปที่เมนู</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();
?>
วิธีใช้งาน
- สร้างฐานข้อมูล
restaurant_db
และรันคำสั่ง SQL - สร้างไฟล์ db.php, menu.php, order.php, kitchen.php, update_status.php
- เปิด
menu.php
ให้ลูกค้าสั่งอาหารจากโต๊ะ - เปิด
kitchen.php
ให้ครัวดูรายการอาหารที่ต้องทำ - ใช้
update_status.php
ในการเปลี่ยนสถานะของอาหาร - ✅ ระบบสั่งอาหารพร้อมใช้งาน!