Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 27, 2023 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/9707804b28ebd9593bdcee243dd98e06 to your computer and use it in GitHub Desktop.
Save code-boxx/9707804b28ebd9593bdcee243dd98e06 to your computer and use it in GitHub Desktop.
PHP MYSQL Seat Reservation

PHP MYSQL SEAT RESERVATION SYSTEM

https://code-boxx.com/seat-reservation-php-mysql/

NOTES

  1. Create a test database and import 1-database.sql.
  2. Open 2-reserve-lib.php and change the database settings to your own.
  3. Access 3a-reservation.php in the browser.

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) SEATS
CREATE TABLE `seats` (
`seat_id` varchar(16) NOT NULL,
`room_id` varchar(16) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `seats`
ADD PRIMARY KEY (`seat_id`,`room_id`);
-- (B) SESSIONS
CREATE TABLE `sessions` (
`session_id` bigint(20) NOT NULL,
`room_id` varchar(16) NOT NULL,
`session_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `sessions`
ADD PRIMARY KEY (`session_id`),
ADD KEY `room_id` (`room_id`),
ADD KEY `session_date` (`session_date`);
ALTER TABLE `sessions`
MODIFY `session_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
-- (C) RESERVATIONS
CREATE TABLE `reservations` (
`session_id` bigint(20) NOT NULL,
`seat_id` varchar(16) NOT NULL,
`user_id` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `reservations`
ADD PRIMARY KEY (`session_id`,`seat_id`,`user_id`);
-- (D) DUMMY DATA
-- (D1) DUMMY SEATS
INSERT INTO `seats` (`seat_id`, `room_id`) VALUES
('A1', 'ROOM-A'),
('A2', 'ROOM-A'),
('A3', 'ROOM-A'),
('A4', 'ROOM-A'),
('B1', 'ROOM-A'),
('B2', 'ROOM-A'),
('B3', 'ROOM-A'),
('B4', 'ROOM-A'),
('C1', 'ROOM-A'),
('C2', 'ROOM-A'),
('C3', 'ROOM-A'),
('C4', 'ROOM-A');
-- (D2) DUMMY SESSION
INSERT INTO `sessions` (`session_id`, `room_id`, `session_date`) VALUES
(1, 'ROOM-A', '2077-06-05 08:00:00');
-- (D3) DUMMY RESERVATION
INSERT INTO `reservations` (`session_id`, `seat_id`, `user_id`) VALUES
('1', 'B2', '555'),
('1', 'A4', '888');
<?php
class Reserve {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $error = null;
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) HELPER FUNCTION - RUN SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) GET SEATS FOR GIVEN SESSION
function get ($sessid) {
$this->query(
"SELECT sa.`seat_id`, r.`user_id` FROM `seats` sa
LEFT JOIN `sessions` se USING (`room_id`)
LEFT JOIN `reservations` r USING(`seat_id`)
WHERE se.`session_id`=?
ORDER BY sa.`seat_id`", [$sessid]
);
$sess = $this->stmt->fetchAll();
return $sess;
}
// (E) SAVE RESERVATION
function save ($sessid, $userid, $seats) {
$sql = "INSERT INTO `reservations` (`session_id`, `seat_id`, `user_id`) VALUES ";
$data = [];
foreach ($seats as $seat) {
$sql .= "(?,?,?),";
array_push($data, $sessid, $seat, $userid);
}
$sql = substr($sql, 0, -1);
$this->query($sql, $data);
return true;
}
}
// (F) 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", "");
// (G) NEW CATEGORY OBJECT
$_RSV = new Reserve();
<!DOCTYPE html>
<html>
<head>
<title>Seat Reservation</title>
<meta charset="utf-8">
<script src="3b-reservation.js"></script>
<link rel="stylesheet" href="3c-reservation.css">
</head>
<body>
<?php
// (A) FIXED IDS FOR THIS DEMO
$sessid = 1;
$userid = 999;
// (B) GET SESSION SEATS
require "2-reserve-lib.php";
$seats = $_RSV->get($sessid);
?>
<!-- (C) DRAW SEATS LAYOUT -->
<div id="layout"><?php
foreach ($seats as $s) {
$taken = is_numeric($s["user_id"]);
printf("<div class='seat%s'%s>%s</div>",
$taken ? " taken" : "",
$taken ? "" : " onclick='reserve.toggle(this)'",
$s["seat_id"]
);
}
?></div>
<!-- (D) LEGEND -->
<div id="legend">
<div class="seat"></div> <div class="txt">Open</div>
<div class="seat taken"></div> <div class="txt">Taken</div>
<div class="seat selected"></div> <div class="txt">Your Selected Seats</div>
</div>
<!-- (E) SAVE SELECTION -->
<form id="ninja" method="post" action="4-save.php">
<input type="hidden" name="sessid" value="<?=$sessid?>">
<input type="hidden" name="userid" value="<?=$userid?>">
</form>
<button id="go" onclick="reserve.save()">Reserve Seats</button>
</body>
</html>
var reserve = {
// (A) CHOOSE THIS SEAT
toggle : seat => seat.classList.toggle("selected"),
// (B) SAVE RESERVATION
save : () => {
// (B1) GET SELECTED SEATS
var selected = document.querySelectorAll("#layout .selected");
// (B2) ERROR!
if (selected.length == 0) { alert("No seats selected."); }
// (B3) NINJA FORM SUBMISSION
else {
var ninja = document.getElementById("ninja");
for (let seat of selected) {
let input = document.createElement("input");
input.type = "hidden";
input.name = "seats[]";
input.value = seat.innerHTML;
ninja.appendChild(input);
}
ninja.submit();
}
}
};
/* (A) ENTIRE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
/* (B) SEATS LAYOUT */
#layout {
max-width: 450px;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
margin-bottom: 30px;
}
.seat {
text-align: center;
padding: 20px 0;
background: #a4bcff;
}
#layout .seat {
cursor: pointer;
}
.taken {
color: #a9a9a9;
background: #f1f1f1 !important;
}
.selected {
background: #ffe319 !important;
}
/* (C) LEGEND */
#legend {
display: grid;
grid-template-columns: 100px 200px;
margin-bottom: 30px;
}
#legend div {
padding: 10px;
}
/* (D) "CHECKOUT" */
#go {
border: 0;
padding: 20px;
font-weight: 700;
color: #fff;
background: #9f3737;
cursor: pointer;
}
<?php
// (A) LOAD LIBRARY
require "2-reserve-lib.php";
// (B) SAVE
$_RSV->save($_POST["sessid"], $_POST["userid"], $_POST["seats"]);
echo "SAVED";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment