I??�m working on a reservation system for a camping website. There are several places where you can sit, but it shouldn't be possible to have 2 different family??�s on one place.
That??�s why I want to make an if statement to check if the given date is already booked at the places, or is available at any of the places.
If one place is booked, I want it to check for another available spot with the same time, if no spots around the same time are available it should say "There's no available spots around this time", if there does turn out to be 1 spot available, it should automatically reserve at that spot and tell the user.
What kind of if statement do I need to use?
Few notes:
Begintijd = starting time
Eindtijd = endtime
reserveringen = reservations
reserveringstijden = reservationtimes
<?php
if (!isset($_SESSION['id'])){
header('Location: login.php');
}
$errors = [];
if(isset($_POST['submit'])) {
if ($_POST['begintijd'] == '') {
$errors['begintijd'] = "Wat is de eerste dag dat je op de camping wilt staan? ontbreekt";
}
if ($_POST['eindtijd'] == '') {
$errors['eindtijd'] = "Wat is de laatste dag dat je op de camping wilt staan? ontbreekt";
}
if (count($errors) == 0) {
//Hier haal je de plaatsen op
$sql = "SELECT * FROM reservering WHERE type = ? ";
$stmt = $conn->prepare($sql);
$stmt->execute([$_POST['type']]);
$result = $stmt->fetchAll();
//Hier haal je de reserveringstijden op
$sql1 = "SELECT * FROM reserveringstijden";
$stmt1 = $conn->prepare($sql1);
$stmt1->execute();
$result1 = $stmt1->fetchAll();
$begintijd = strtotime($_POST['begintijd']);
$eindtijd = strtotime($_POST['eindtijd']);
var_dump($_POST['begintijd']);
if (($begintijd <= $result1[0]['begintijd'])&&($begintijd < $result1[0]['eindtijd']))
{
echo "hallo?";
exit;
if (($eindtijd = $result1[0]['begintijd'])&&($begintijd < $result1[0]['eindtijd']))
{
echo "stap verder";
exit;
}
}
else
{
echo "mag niet hier komen";
exit;
/*$sql2 = "INSERT INTO `reserveringstijden`(`id_plaats`, `begintijd`, `eindtijd`, `id_kampeerder`) VALUES (?,?,?,?) ";
$stmt2 = $conn->prepare($sql2);
$stmt2->execute([$result[0]['id'], $_POST['begintijd'], $_POST['eindtijd'], $_SESSION['id']]); */
}
}
}
?>
<p>U wilt reserveren: </p>
<div class="blockform">
<form method="post">
<label for="Plaats">Waar slaap je in?</label><br>
<select id="type" name="type"><br>
<option value="tent">tent</option>
<option value="camper">camper</option>
</select>
<label>Eerste dag</label><br>
<input type="date" id="begintijd" name="begintijd" value=" <?php echo (isset ($_POST['begintijd']) ? $_POST['begtintijd'] : ''); ?> "><br>
<label>Laatste dag</label><br>
<input type="date" id="eindtijd" name="eindtijd" value=" <?php echo (isset ($_POST['eindtijd']) ? $_POST['eindtijd'] : ''); ?> "> <br>
<button type="submit" name="submit">reserveren</button>
</form>
</div>
Let me assume that you have two parameters, say$begin
and$end
, areservations
table that has columnsbegin_time
andend_time
, and aspaces
table that has the spaces.
Then the logic for overlaps looks like this:
where $begin < r.end_time and
$end > r.begin_time;
Two time periods overlaps if one begin before the second ends and the first ends after the second begins.
You can return the spaces that are available during a period of time usingnot exists
:
select s.*
from spaces s
where not exists (select 1
from reservations r
where r.space_id = s.space_id and
$begin < r.end_time and
$end > r.begin_time
);
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/
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.