danish hashmi
danish hashmi

Reputation: 484

logic for a php function

i need to make a php code for checking hotel room avaibility where user from the present day can book rooms upto 90 days or less and there are total 30 rooms available in the hotel,so if once i store the data for a user like his booking from one date till another next time if i want to check the avaibility how should i do it in php,what would be the logic.

obviously i simple query like this isn't correct for eg

$this->db->select('*')
    ->from('default_bookings')
    ->where('booking_from <',$input['fromdate'])
    ->where('booking_till >',$input['tilldate']);

Upvotes: 1

Views: 378

Answers (3)

user3180582
user3180582

Reputation: 261

This works for me. If the result is 0, then there is no booking in interval. The day of arrival can be same as day of departure.

        SELECT COUNT(*)
        FROM booking
        WHERE room_id = :room_id
        AND (
            (date_to > :start AND date_to < :end)
            OR
            (date_from > :start AND date_from < :end)
            OR
            (date_from < :start AND date_to > :end)
        ) 

Upvotes: 0

Ron
Ron

Reputation: 1336

I assume, you want to find free space in you booking-calendar.

So you have a start-date and an end-date. You look in every room and make the following assumptions:

  1. The startdate and/or the enddate should not be present in an confirmed booking-period.
  2. There is no period between the start-date and the end-date.

A SQL-Query could look like this:

Table bookings {
    int booking_id
    int room_id
    datetime reservation_start
    datetime reservation_end
    ...
}

SELECT
    COUNT(*)
FROM
    bookings AS test1
WHERE
    room_id={$roomId}
    AND
    {$startdate} NOT BETWEEN reservation_start AND reservation_end
    AND
    {$enddate} NOT BETWEEN reservation_start AND reservation_end
    AND
    reservation_start NOT BETWEEN {$startdate} AND {$enddate};

Is the result is 0, then there is no intersection with existing reservations.

You can also automate the "looking into rooms" by using this query as a sub-query.

Upvotes: 1

Ron
Ron

Reputation: 1336

This looks like Zend_DB_Select

Try this:

$this->db->select('*')
->form('default_bookings')
->where('booking_from < ?',$input['fromdate'])
->where('booking_till > ?',$input['tilldate']);

The "?" where missing then...

Upvotes: 1

Related Questions