How to check for available places with if statement?

49
June 02, 2021, at 05:50 AM

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>
Answer 1

Let me assume that you have two parameters, say $begin and $end, a reservations table that has columns begin_time and end_time, and a spaces 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 using not 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
                 );
READ ALSO
How to send a email from react (not a contact form)?

How to send a email from react (not a contact form)?

I want to automatically send an email with order details to the user when the order is placedI have only found tutorials, teaching how to enable the user to send an email using a contact form

63
SCSS preprocessing problem using keyframe names

SCSS preprocessing problem using keyframe names

I'm trying to use Scss preprocessor with animations and I stumbled across some kind of error or bugI know how to fix it, but but I want to know what's wrong and what exactly is causing this error

73
QMenu created in a widget outside of my main Window doesn&#39;t appear

QMenu created in a widget outside of my main Window doesn't appear

I've created a QMenu in a widget outside of my main window, but when I try to show it on my Application it simply doesn't appear

62
How to Make a PHP Variable Return Text Instead of Nothing

How to Make a PHP Variable Return Text Instead of Nothing

My code needs to see and use the text held in a variable:

74