Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active February 11, 2024 01:15
Show Gist options
  • Save code-boxx/c122848e75e30b0c6a8e7639e9f80b22 to your computer and use it in GitHub Desktop.
Save code-boxx/c122848e75e30b0c6a8e7639e9f80b22 to your computer and use it in GitHub Desktop.
PHP MYSQL Food Ordering System

PHP MYSQL FOOD ORDERING SYSTEM

https://code-boxx.com/simple-php-food-ordering-system/

IMAGES

p1 p2 p3 p4

NOTES

  1. Create a database and import 1-database.sql.
  2. Change the database settings in 2-lib.php to your own.
  3. Access 3a-kiosk.php for the front-end ordering.
  4. Finally, 4a-admin.php is the dummy demo admin panel.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) MENU
CREATE TABLE `menu` (
`item_id` bigint(20) NOT NULL,
`item_name` varchar(255) NOT NULL,
`item_desc` varchar(255) DEFAULT NULL,
`item_img` varchar(255) DEFAULT NULL,
`item_price` decimal(12,2) NOT NULL DEFAULT 0.00
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `menu`
ADD PRIMARY KEY (`item_id`),
ADD KEY `item_name` (`item_name`);
ALTER TABLE `menu`
MODIFY `item_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
INSERT INTO `menu` (`item_id`, `item_name`, `item_desc`, `item_img`, `item_price`) VALUES
(1, 'Pizza', 'Mamma mia. It\'s a pizza. No pineapple.', 'p1.png', '11.11'),
(2, 'Egg Fried Rice', '3 ingredients only. Egg, fried, rice.', 'p2.png', '22.22'),
(3, 'Miso Ramen', 'Zetai oishii. Instant ja nai.', 'p3.png', '33.33'),
(4, 'Cabbage', 'Vegan friendly option.', 'p4.png', '44.44');
-- (B) ORDERS
CREATE TABLE `orders` (
`order_id` bigint(20) NOT NULL,
`order_date` datetime NOT NULL DEFAULT current_timestamp(),
`order_status` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `orders`
ADD PRIMARY KEY (`order_id`),
ADD KEY `order_date` (`order_date`),
ADD KEY `order_status` (`order_status`);
ALTER TABLE `orders`
MODIFY `order_id` bigint(20) NOT NULL AUTO_INCREMENT;
-- (C) ORDER ITEMS
CREATE TABLE `order_items` (
`order_id` bigint(20) NOT NULL,
`item_id` bigint(20) NOT NULL,
`item_name` varchar(255) NOT NULL,
`item_price` decimal(12,2) NOT NULL,
`item_qty` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `order_items`
ADD PRIMARY KEY (`order_id`,`item_id`);
-- (D) ORDER TOTALS
CREATE TABLE `order_totals` (
`order_id` bigint(20) NOT NULL,
`total_id` bigint(20) NOT NULL,
`total_name` varchar(255) NOT NULL,
`total_amt` decimal(12,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `order_totals`
ADD PRIMARY KEY (`order_id`,`total_id`);
<?php
class Order {
// (A) CONSTRUCTOR - CONNECT TO THE DATABASE
private $pdo = null;
private $stmt = null;
public $error;
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct () {
if ($this->stmt !== null) { $this->stmt = null; }
if ($this->pdo !== null) { $this->pdo = null; }
}
// (C) EXECUTE SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) GET ALL MENU ITEMS
function getMenu () {
$this->query("SELECT * FROM `menu`");
return $this->stmt->fetchAll();
}
// (E) GET ITEMS IN CART
function getCart () {
// (E1) SQL QUERY
$this->query(sprintf(
"SELECT * FROM `menu` WHERE `item_id` IN (%s)",
substr(str_repeat("?,", count($_SESSION["cart"])), 0, -1)
), array_keys($_SESSION["cart"]));
// (E2) GET DATA
$cart = ["i"=>[], "t"=>0];
while ($r = $this->stmt->fetch()) {
$cart["i"][$r["item_id"]] = [
"n" => $r["item_name"], // name
"q" => $_SESSION["cart"][$r["item_id"]], // quantity
"p" => $r["item_price"] * $_SESSION["cart"][$r["item_id"]] // price
];
$cart["t"] += $cart["i"][$r["item_id"]]["p"];
}
return $cart;
}
// (F) CHECKOUT
function checkout () : void {
// (F1) EMPTY CART
if (count($_SESSION["cart"])==0) { exit("Cart is empty."); }
// (F2) "MAIN ORDER"
$this->pdo->beginTransaction();
$this->query("INSERT INTO `orders` (`order_status`) VALUES (0)");
$oid = $this->pdo->lastInsertId();
// (F3) ORDER ITEMS
$cart = $this->getCart();
$i = 1;
foreach ($cart["i"] as $item) {
$this->query(
"INSERT INTO `order_items` (`order_id`, `item_id`, `item_name`, `item_price`, `item_qty`) VALUES (?, ?, ?, ?, ?)",
[$oid, $i, $item["n"], $item["p"], $item["q"]]
);
$i++;
}
// (F4) ORDER TOTALS
$this->query(
"INSERT INTO `order_totals` (`order_id`, `total_id`, `total_name`, `total_amt`) VALUES (?, ?, ?, ?)",
[$oid, 1, "total", $cart["t"]]
);
// (F5) DONE
$this->pdo->commit();
$_SESSION["cart"] = [];
echo "OK";
}
// (G) GET ALL INCOMPLETE ORDERS
function getOrders () {
$orders = [];
$this->query("SELECT * FROM `orders` WHERE `order_status`<2");
foreach ($this->stmt->fetchAll() as $o) {
$orders[$o["order_id"]] = $o;
$this->query("SELECT `item_name`, `item_price`, `item_qty` FROM `order_items` WHERE `order_id`=?", [$o["order_id"]]);
$orders[$o["order_id"]]["i"] = $this->stmt->fetchAll();
$this->query("SELECT `total_name`, `total_amt` FROM `order_totals` WHERE `order_id`=?", [$o["order_id"]]);
$orders[$o["order_id"]]["t"] = $this->stmt->fetchAll();
}
return $orders;
}
// (H) UPDATE ORDER
function update ($id) : void {
$this->query(
"UPDATE `orders` SET `order_status`=`order_status`+1 WHERE `order_id`=?",
[$id]
);
}
}
// (I) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (J) ORDER STATUS
define("ORDER_STAT", [
0 => "Preparing", 1 => "Ready", 2 => "Delivered"
]);
// (K) START!
$_ORD = new Order();
session_start();
if (!isset($_SESSION["cart"])) { $_SESSION["cart"] = []; }
<!DOCTYPE html>
<html>
<head>
<title>Ordering Kiosk</title>
<meta charset="utf-8">
<link rel="stylesheet" href="3x-kiosk.css">
<script src="3b-kiosk.js"></script>
</head>
<body>
<!-- (A) MENU ITEMS -->
<div id="menu"><?php
require "2-lib.php";
foreach ($_ORD->getMenu() as $item) { ?>
<div class="item">
<img class="item-img" src="<?=$item["item_img"]?>">
<div class="item-name">
<?=$item["item_name"]?>
$<?=$item["item_price"]?>
</div>
<div class="item-desc"><?=$item["item_desc"]?></div>
<input class="item-add" type="button" value="Add To Cart" onclick="cart.add(<?=$item["item_id"]?>)">
</div>
<?php } ?>
</div>
<!-- (B) CART -->
<div id="cart"></div>
</body>
</html>
var cart = {
// (A) PROPERTIES
hCart : null, // html cart div
// (B) AJAX HELPER
ajax : opt => {
// (B1) FORM DATA
let form = new FormData();
for (let [k,v] of Object.entries(opt.data)) {
form.append(k, v);
}
// (B2) AJAX FETCH
fetch("3c-cart.php", { method:"post", body:form })
.then(res => res.text())
.then(res => opt.ok(res))
.catch(err => console.error(err));
},
// (C) CART INIT
init : () => {
cart.hCart = document.getElementById("cart");
cart.list();
},
// (D) LIST CART
list : () => cart.ajax({
data : { req : "list" },
ok : res => cart.hCart.innerHTML = res
}),
// (E) ADD ITEM TO CART
add : id => cart.ajax({
data : { req : "add", id : id },
ok : res => cart.list()
}),
// (F) SET ITEM QUANTITY
set : (id, qty) => cart.ajax({
data : { req : "set", id : id, qty : qty },
ok : res => cart.list()
}),
// (G) REMOVE ITEM FROM CART
del : id => cart.ajax({
data : { req : "del", id : id },
ok : res => cart.list()
}),
// (H) CHECKOUT
checkout : () => cart.ajax({
data : { req : "checkout" },
ok : res => {
if (res=="OK") {
alert("Checkout successful!");
// @TODO - UP TO YOU
} else { alert(res); }
cart.list();
}
})
};
window.onload = cart.init;
<?php
require "2-lib.php";
if (isset($_POST["req"])) { switch($_POST["req"]) {
// (A) LIST CART ITEMS
case "list":
if (count($_SESSION["cart"]) > 0) {
// (A1) CART ITEMS
$cart = $_ORD->getCart();
foreach($cart["i"] as $id=>$item) { ?>
<div class="cart-row">
<div class="cart-del" onclick="cart.del(<?=$id?>)">X</div>
<div class="cart-item">
<?=$item["n"]?>
$<?=$item["p"]?>
</div>
<input class="cart-qty" type="number" value="<?=$item["q"]?>" min="1" max="99" onchange="cart.set(<?=$id?>, this.value)">
</div>
<?php }
// (A2) TOTAL
printf("<div class='cart-row total'>
<div class='cart-item'>Total</div>
<div>$%.2f</div>
</div>", $cart["t"]);
// (A3) CHECKOUT
echo "<input class='cart-checkout' type='button' value='Checkout' onclick='cart.checkout()'>";
} else { echo "<div class='cart-row'>Cart is empty.</div>"; }
break;
// (B) ADD ITEM TO CART
case "add":
if (isset($_SESSION["cart"][$_POST["id"]])) {
$_SESSION["cart"][$_POST["id"]]++;
} else {
$_SESSION["cart"][$_POST["id"]] = 1;
}
break;
// (C) SET QUANTITY
case "set":
$_SESSION["cart"][$_POST["id"]] = $_POST["qty"];
break;
// (D) REMOVE ITEM
case "del":
unset($_SESSION["cart"][$_POST["id"]]);
break;
// (E) CHECKOUT
case "checkout":
$_ORD->checkout();
break;
}}
/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
max-width: 1000px;
margin: 0 auto;
padding: 10px;
background: #333;
}
input[type=button] {
font-weight: 700;
color: #fff;
background: #a32626;
border: 0;
padding: 10px;
cursor: pointer;
}
/* (B) ITEMS */
#menu {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-gap: 10px;
}
@media screen AND (max-width: 768px) {
#menu { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.item {
padding: 10px;
background: #fff;
}
.item-img { max-width: 100%; }
.item-name {
color: #e54242;
font-weight: 700;
}
.item-desc {
color: #2a2a2a;
font-size: 14px;
margin-bottom: 10px;
}
/* (C) CART */
#cart { margin-top: 10px; }
#cart .cart-row {
display: flex;
align-items: center;
padding: 10px; margin-bottom: 5px;
color: #fff;
border: 1px solid #000;
background: #242424;
}
.cart-del {
color: #e54242;
cursor: pointer;
}
.cart-item {
flex-grow: 1;
padding: 0 10px;
}
.cart-qty {
width: 70px;
padding: 5px;
}
.cart-row.total {
font-weight: 700;
}
.cart-checkout {
margin-top: 10px;
}
<!DOCTYPE html>
<html>
<head>
<title>Ordering Kiosk Admin</title>
<meta charset="utf-8">
<link rel="stylesheet" href="4x-admin.css">
<script src="4b-admin.js"></script>
</head>
<body>
<!-- (A) UPDATE ORDER -->
<form id="upform" method="post">
<input type="hidden" id="upid" name="id">
</form>
<?php
require "2-lib.php";
if (isset($_POST["id"])) { $_ORD->update($_POST["id"]); }
?>
<!-- (B) ORDERS LIST -->
<div id="orders"><?php
foreach ($_ORD->getOrders() as $id=>$o) { ?>
<div class="order" onclick="up(<?=$id?>)">
<!-- (B1) ORDER DATA -->
<div class="order-id">#<?=$id?></div>
<div>
<div class="order-stat"><?=ORDER_STAT[$o["order_status"]]?></div>
</div>
<div class="order-time"><?=$o["order_date"]?></div>
<!-- (B2) ORDER ITEMS -->
<ul class="order-items"><?php
foreach ($o["i"] as $i) {
printf("<li>%s x %d</li>", $i["item_name"], $i["item_qty"]);
}
?></ul>
</div>
<?php }
?></div>
</body>
</html>
function up (id) {
document.getElementById("upid").value = id;
document.getElementById("upform").submit();
}
/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
max-width: 1000px;
margin: 0 auto;
padding: 10px;
background: #333;
}
input[type=button] {
font-weight: 700;
color: #fff;
background: #a32626;
border: 0;
padding: 10px;
cursor: pointer;
}
/* (B) ORDERS */
#orders {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
grid-gap: 10px;
}
@media screen AND (max-width: 768px) {
#orders { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.order {
padding: 10px;
background: #fff;
}
.order-stat {
display: inline-block;
padding: 5px;
margin-bottom: 5px;
border-radius: 5px;
font-size: 12px;
font-weight: 700;
color: #fff;
background: #9b1919;
}
.order-id {
font-size: 24px;
font-weight: 700;
}
.order-items {
padding: 5px;
margin: 0;
}
.order-time {
font-weight: 700;
}
.order-items li {
list-style-type: none;
margin-top: 5px;
padding: 5px;
border: 1px solid #eee;
background: #f7f7f7;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment