I'm building an appointment system to a barbershop. It works like this:
My problem: Lets say that the barber has 8AM and 9PM work hours. A client wants 8AM appointment, and the next one that will make the appointment should not see that the 8AM "space" is available, only 9AM and posterior hours.
How can I do that? I thought that I would make an code that reads all data from the db to that day and fetch all taken hours.
Reservation form:
<!DOCTYPE html>
<html>
<head>
<title>
Agendar
</title>
<link href="theme.css" rel="stylesheet">
</script>
</head>
<body>
<?php
// (A) PROCESS RESERVATION
if (isset($_POST['date'])) {
require "reserve.php";
if ($_RSV->save(
$_POST['date'], $_POST['slot'], $_POST['name'], $_POST['email'],
$_POST['tel'], $_POST['notes'])) {
echo "<div class='ok'>Reserva feita! Entraremos em contato caso haja necessidade.</div>";
} else { echo "<div class='err'>".$_RSV->error."</div>"; }
}
?>
<!-- (B) RESERVATION FORM -->
<h1>Agendar um hor??rio</h1>
<form id="resForm" method="post" target="_self">
<label for="res_name">Nome</label>
<input type="text" required name="name" placeholder="Seu nome"/>
<label for="res_tel">Telefone</label>
<input type="text" required name="tel" placeholder="(12) 12345-6789"/>
<label for="res_notes">Barba, cabelo ou os dois?</label>
<select required name="notes">
<option disabled selected value> -- Selecione -- </option>
<option value="Barba">Barba</option>
<option value="Cabelo">Cabelo</option>
<option value="Cabelo e Barba">Cabelo e Barba</option>
</select>
<label>Date</label>
<input type="date" required id="res_date" name="date" placeholder="<?=date("Y-m-d")?>">
<label>Hor??rio</label>
<select required name="slot">
<option disabled selected value> -- Selecione -- </option>
<option value="8:00">8:00</option>
<option value="9:00">9:00</option>
</select>
<input type="submit" value="Reservar"/>
</form>
</body>
</html>
Admin page:
<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM reservations");
?>
<!DOCTYPE html>
<html>
<head>
<link href="styles.css" rel="stylesheet">
<title> Agendamentos</title>
</head>
<body>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<table>
<tr>
<td>ID</td>
<td>Data</td>
<td>Hor??rio</td>
<td>Nome</td>
<td>Telefone</td>
<td>Vai fazer</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["res_id"]; ?></td>
<td><?php echo $row["res_date"]; ?></td>
<td><?php echo $row["res_slot"]; ?></td>
<td><?php echo $row["res_name"]; ?></td>
<td><?php echo $row["res_tel"]; ?></td>
<td><?php echo $row["res_notes"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</body>
</html>
Structure:
CREATE TABLE `reservations` (
`res_id` int(11) NOT NULL AUTO_INCREMENT,
`res_date` date DEFAULT NULL,
`res_slot` varchar(32) DEFAULT NULL,
`res_name` varchar(255) NOT NULL,
`res_tel` varchar(60) NOT NULL,
`res_notes` text DEFAULT NULL,
`res_email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`res_id`),
KEY `res_date` (`res_date`),
KEY `res_slot` (`res_slot`),
KEY `res_name` (`res_name`),
KEY `res_tel` (`res_tel`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin
First yourreservations
table should not contain users' data likename
,tel
,email
instead you just createclients
table and store each client as a single row. Next In yourreservations
table add columnclient_id
of integer type, where you'll keep numeric id of client in theclients
table. This way you do not repeat client's data. You can fetch data from many tables in one query withJOIN
statement. That's one-to-one relation (one reservation can have only one client).
Rest is rather easy, when you generating list of available
select * from reservations where client_id > 0
Tip: as a programmer it will be easier to store hours in DB in 24h format, that way you'll use8:00
or16:00
instead of8
+AM
vs8
+PM
. Of course at the end you can display8:00 PM
on the page.
CREATE TABLE `reservations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`res_date` date DEFAULT NULL,
`slot` time DEFAULT NULL,
`client_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
INSERT INTO reservations (res_date, slot, client_id) VALUES ('2021-02-25', '08:00:00', 1);
INSERT INTO reservations (res_date, slot, client_id) VALUES ('2021-02-26', '11:30:00', 2);
INSERT INTO reservations (res_date, slot, client_id) VALUES ('2021-02-27', '16:30:00', 3);
CREATE TABLE `clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) DEFAULT NULL,
`last_name` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`phone` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
INSERT INTO clients (first_name, last_name, email, phone) VALUES ('John', 'Doe', '[email protected]', '+1234');
INSERT INTO clients (first_name, last_name, email, phone) VALUES ('Jane', 'Doe', '[email protected]', '+4321(7)');
INSERT INTO clients (first_name, last_name, email, phone) VALUES ('Johan', 'M??ller', 'mueller@foode', '+49123');
JOIN
- querying connected tables at once:
SELECT reservations.*, clients.*
FROM reservations
LEFT JOIN clients ON (reservations.client_id = clients.id)
you have to check the available hours.
1- a functin to create times between 8:00 to 21:00
function dailyTime(){
$time = [];
$start = 8;
$end = 21;
for ($time = $start; $time <= $end; $time++) {
array_push($time,date("H:00", mktime($time+1)));
}
return $time;
}
2- do the filteration for options:
a) generate time between 8 - 21 b) unset reserved time from array c) make select box for the rest of items left in time array
<select required name="slot">
<option disabled selected value> -- Selecione -- </option>
<?php
$availableTimes = dailyTime();
$result = mysqli_query($conn,"SELECT * FROM reservations");
while($row = mysqli_fetch_array($result)) {
if (($key = array_search($row['res_slot'], $availableTimes)) !== false) {
unset($availableTimes[$key]);
}
}
foreach($availableTimes as $free){
echo '<option>'.$free.'<option>';
}
?>
</select>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.